2013-07-26 51 views
1

我想从Java代码传递两个参数,以它来启动一个jar文件推出罐子,我曾尝试是:Java代码的参数传递给它

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"); 
} 

代码是完美的发射罐, 但我想使用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)如果路径环境变量包含64位JVM位置如果我只是给java.exe

我想这下面的代码,但我无法使用这种方法

String command = {"rundll32 url.dll,FileProtocolHandler ", jarFile + "" , arg1, arg2} 
Process p = Runtime 
        .getRuntime() 
        .exec(command); 

那么,有没有任何其他方式传递参数给它推出的jar文件来传递参数?

+0

'rundll32 url.dll,FileProtocolHandler'应该只有一个参数,它是你想要协议处理程序的文件的名称。如果你在调用它时忽略了'arg1'和'arg2'会发生什么? – mthmulders

+0

java中的Java,这是* inception * ..为什么不使用jar作为依赖项,并在需要时直接从jar中运行main类? – 2013-07-26 10:57:01

+0

@mthmulders:我想将arg1和arg2传递给第二个jar文件,没有第二个jar文件就不能工作。我的整个功能都存在于第二个jar文件 –

回答

1

使用参数'-d32',即 java -d32 -jar jarfile arg1 arg2。

java -help也可以得心应手。