[英]java code to launch jar by passing arguments to it
我想通过从Java代码传递两个参数来启动一个jar文件,我尝试过的是:
File jarFile = new File("path/to/jar/file");
if ((jarFile).exists()) {
String[] command = new String[5];
command[0] = "java.exe";
command[1] = "-jar";
command[2] = jarFile + "";
command[3] = "arg1";
command[4] = "arg2";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} else {
System.out.println("File is not available");
}
代码非常适合启动jar,但是我想使用32位JVM启动jar文件,因为我的驱动程序仅为32位。
而且我的机器同时包含32位和64位JVM,为了使用32位JVM启动jar,我可能需要使用硬编码的java.exe位置,例如
command[0] = "C:\\Program Files (x86)\\Java\\jdk1.7.0_25\\bin\\java.exe"
但
1)如果最终用户在其他位置安装了Java,该怎么办?
2)如果我只给java.exe, 路径环境变量包含64位JVM位置怎么办?
我尝试下面的代码,但我无法使用此方法传递参数
String command = {"rundll32 url.dll,FileProtocolHandler ", jarFile + "" , arg1, arg2}
Process p = Runtime
.getRuntime()
.exec(command);
那么,还有其他方法可以通过传递参数来启动jar文件吗?
使用参数“ -d32”,即java -d32 -jar jarfile arg1 arg2。
java -help也可以派上用场。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.