2013-10-06 20 views
2

我的應用程序在用戶創建.bat文件Startup目錄能夠自動運行時,在用戶登錄Java中創建批處理文件和逃生空間

我這是在當前如何創建.bat

File startupFile=getStartupFile(); 
PrintWriter out=new PrintWriter(new FileWriter(startupFile)); 
out.println("@echo off"); 
out.println("start " + System.getProperty("user.dir") + fileSeparator +"MyApp.exe"); 
out.println("exit"); 
out.close(); 
} 

BTW:startupFile只是位置到Startup目錄

的問題似乎是,有時System.getProperty("user.dir")包含在路徑中有空格。例如,第二條線可以是:

start C:\Program Files (x86)\MyApp\MyApp.exe 

當它試圖找到運行應用程序這打破了.bat文件。

任何想法如何使.bat知道在哪裏可以找到應用程序?無論它在哪裏安裝?

回答

2

報價在任何情況下該文件的位置(帶或不帶空格)應該工作

out.println("start \"" + System.getProperty("user.dir") + fileSeparator +"MyApp.exe\""); 
+0

這隻能打開一個空cmd窗口,couldent得到它的工作 – Alosyius

+0

@Alosyius什麼是你'MyApp.exe'做? –

+0

它是一個java可執行文件,與launch4j打包到一個.exe – Alosyius