[英]Java Virtual Machine Error When Trying To Run an Application From Java
I am trying to run an .exe from a java application, but I am getting this error message: 我正在尝试从Java应用程序运行.exe,但是却收到此错误消息:
Java Virtual Machine Launcher(title) A Java Exception has occurred. Java虚拟机启动器(标题)发生Java异常。
I have tried: 我努力了:
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("C:\\PathToExe\\MyExe.exe");
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();
} catch (Exception exc) {}
And: 和:
try {
Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(ScannerUI.class.getName()).log(Level.SEVERE, null, ex);
}
Both of them work when I try to run something else like uTorrent, but it fails when I try to run the .exe that I have to. 当我尝试运行uTorrent之类的东西时,它们都可以工作,但是当我尝试运行必须的.exe时,它们都失败了。
Also, this .exe is in a folder and if I try to run this .exe alone (out of it's folder) it returns the same error message. 另外,此.exe位于文件夹中,如果我尝试单独运行此.exe(不在其文件夹中),它将返回相同的错误消息。
I don't think the code above is wrong, but it's missing something to be able to run my .exe. 我不认为上面的代码是错误的,但是它缺少能够运行我的.exe的东西。
New CODE: 新代码:
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("C:\\PathToExe\\MyExe.exe");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Exception e)
{
e.printStackTrace();
}
New Output: 新输出:
<ERROR>
Exception in thread "main" java.lang.NoClassDefFoundError: **org/lwjgl/LWJGLException**
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
</ERROR>
Process exitValue: 0
Quoting you: 报价给你:
Also, this .exe is in a folder and if I try to run this .exe alone (out of it's folder) it returns the same error message.
另外,此.exe位于文件夹中,如果我尝试单独运行此.exe(不在其文件夹中),它将返回相同的错误消息。
Most likely you are forgetting to set appropriate directory. 您很可能忘记设置适当的目录。 Use ProcessBuilder instead of
Runtime.exec()
, and make a call to ProcessBuilder.directory(File)
method before ProcessBuilder.start()
. 使用ProcessBuilder而不是
Runtime.exec()
,并在ProcessBuilder.start()
之前调用ProcessBuilder.directory(File)
方法。
See this question for more information. 有关更多信息,请参见此问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.