2010-10-26 137 views
3

我有一個即將開始使用Web Start部署的Java應用程序。但是新的需求使我重新思考了這一點,因爲我現在需要添加一項功能,允許最終用戶選擇是否希望在啓動時運行此程序(Windows,而不是跨平臺)。但是我仍然希望避免將此作爲服務。有什麼方法可以使用Web Start來完成,或者我應該探索其他選項來部署它嗎?提前致謝。在Windows啓動時部署Java Web Start

回答

1

我還沒有嘗試過,但是我想知道是否可以將新的JNLP IntegrationServicejavaws命令行程序結合使用。這個想法是以編程方式在Windows啓動組中創建一個快捷方式(儘管該位置取決於特定的Windows版本)。

+0

嗯......有趣的想法。謝謝,我會努力的。 – Glockstock 2010-10-26 19:50:21

+0

有人試過這種方法嗎? – 2014-03-15 20:31:13

3

它的實際工作,以把這個在JNLP文件:

<shortcut online="true"> 
    <desktop/> 
    <menu submenu="Startup"/> 
</shortcut> 

但仍然只能用英文版的Windows版本。德語是「Autostart」,西班牙語是我認爲的「Iniciar」。所以它通過IntegrationService的方式導致基本相同的頭痛。

1

要解決啓動文件夾的語言問題,只需使用註冊表。這是一些應該可以工作的代碼。這會調用reg.exe來進行註冊表更改。

public class StartupCreator { 

    public static void setupStartupOnWindows(String jnlpUrl, String applicationName) throws Exception { 
     String foundJavaWsPath = findJavaWsOnWindows(); 
     String cmd = foundJavaWsPath + " -Xnosplash \"" + jnlpUrl + "\""; 
     setRegKey("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", applicationName, cmd); 
    } 

    public static String findJavaWsOnWindows() { 
    // The paths where it will look for java 
    String[] paths = { 
     // first use the JRE that was used to launch this app, it will probably not reach the below paths 
     System.getProperty("java.home") + File.separator + "bin" + File.separator + "javaws.exe", 
     // it must check for the 64 bit path first because inside a 32-bit process system32 is actually syswow64 
     // 64 bit machine with 32 bit JRE 
     System.getenv("SYSTEMROOT") + File.separator + "syswow64" + File.separator + "javaws.exe", 
     // 32 bit machine with 32 bit JRE or 64 bit machine with 64 bit JRE 
     System.getenv("SYSTEMROOT") + File.separator + "system32" + File.separator + "javaws.exe",}; 
     return findJavaWsInPaths(paths); 
    } 

    public static String findJavaWsInPaths(String[] paths) throws RuntimeException { 
     String foundJavaWsPath = null; 
     for (String p : paths) { 
      File f = new File(p); 
      if (f.exists()) { 
       foundJavaWsPath = p; 
       break; 
      } 
     } 
     if (foundJavaWsPath == null) { 
      throw new RuntimeException("Could not find path for javaws executable"); 
     } 
     return foundJavaWsPath; 
    } 

    public static String setRegKey(String location, String regKey, String regValue) throws Exception { 
     String regCommand = "add \"" + location + "\" /v \"" + regKey + "\" /f /d \"" + regValue + "\""; 

     return doReg(regCommand); 
    } 

    public static String doReg(String regCommand) throws Exception { 

     final String REG_UTIL = "reg"; 
     final String regUtilCmd = REG_UTIL + " " + regCommand; 
     return runProcess(regUtilCmd); 
    } 

    public static String runProcess(final String regUtilCmd) throws Exception { 
     StringWriter sw = new StringWriter(); 
     Process process = Runtime.getRuntime().exec(regUtilCmd); 

     InputStream is = process.getInputStream(); 
     int c = 0; 
     while ((c = is.read()) != -1) { 
      sw.write(c); 
     } 
     String result = sw.toString(); 
     try { 
      process.waitFor(); 
     } catch (Throwable ex) { 
      System.out.println(ex.getMessage()); 
     } 
     if (process.exitValue() == -1) { 
      throw new Exception("REG QUERY command returned with exit code -1"); 
     } 
     return result; 
    } 
} 
+0

This Works!也許有點慢(應用程序幾秒鐘沒有響應,我想這可能是因爲這個代碼),但工作。現在我需要將自己的應用程序本身放在自動啓動中(不通過JNLP文件)... – 2014-03-23 22:14:26

+0

通過SwingWorker運行代碼,它將在後臺運行。 – 2014-03-24 17:02:59

+0

是的,我在一個單獨的線程運行它!謝謝! – 2014-03-25 18:03:43