我正在尝试从 jar 文件本身调用一个打包在 jar 文件中的批处理文件。我不完全确定这是可能的,但我想如果有人知道这里会有人。
import java.io.IOException;
import java.net.URISyntaxException;
public class FileLocationFinder {
public static void main(String[] args) throws IOException, URISyntaxException {
String curdir = System.getProperty("user.dir");
System.out.println("The User.dir = " + curdir);
File newFile = new File (FileLocationFinder.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String strNewFile = newFile.toString();
System.out.println("The New Path = " + strNewFile);
String abPath = new File(".").getAbsolutePath();
System.out.println("The absolutePath = " + abPath);
String conPath = new File(".").getCanonicalPath();
System.out.println("The Canonical Path = " + conPath);
runDir(strNewFile);
}
public static void runDir(String dir){
final int exitVal;
final Process dirprocess;
try {
System.out.println("Running from curdir");
dirprocess = Runtime.getRuntime().exec(dir + "\\" + "SomeBatch.bat");
try {
exitVal = dirprocess.waitFor();
if(exitVal == 0){
System.out.println("Dir worked");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
SomeBatch.bat 的内容是
echo I was called
当我编译然后运行时,它会按照我的预期运行。但是当我创建 jar 文件时,我得到了很多错误。