0

I am trying to run an .exe from a java application, but I am getting this error message:

Java Virtual Machine Launcher(title) A Java Exception has occurred.

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.

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.

I don't think the code above is wrong, but it's missing something to be able to run my .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
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Could you show the full error message and call stack. – Tarik Aug 19 '13 at 16:27
  • Well, that is the full error message. Where in my code should I call Stack? Thank you for your fast reply. @Tarik – Herbert Souza Silva Aug 19 '13 at 16:39
  • 2
    HAHA, that was gold! Sorry for being unprofessional, but this is funny right here. – Georgian Aug 19 '13 at 16:56
  • Herbert, the call stack is the chain of Method calls. The stack trace shows the chain of method calls that lead to the error and includes the offending line. See http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#printStackTrace(). Note that most IDE's print the stack trace upon unhandled errors. – Tarik Aug 19 '13 at 17:38
  • 1) Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. 2) Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..` – Andrew Thompson Aug 19 '13 at 18:24
  • Alright, I did what you guys told me to. Now we can see that the Exception is about LWJGL (which is a java library for games). The application I am trying to run is a game based on Java, just like Minecraft. @AndrewThompson – Herbert Souza Silva Aug 22 '13 at 21:45
  • So.. `MyExe.exe` is actually a Java app. that is supposed to access the LWJGL API? Does your `exe` work when you double click it, or (better, for output) launch it from the command line? – Andrew Thompson Aug 22 '13 at 21:57
  • @Herbert yes you need to debug the exe first; your calling app isn't the main problem here (although you'll want a separate thread for your stream reader). – Will Aug 22 '13 at 22:06

1 Answers1

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.

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().

See this question for more information.

Community
  • 1
  • 1
fernacolo
  • 7,012
  • 5
  • 40
  • 61
  • Bingo! Should I post the final result? Only one more question, just for curiosity... is it possible to run the applicative in windows mode? Thank you! @fernacolo – Herbert Souza Silva Aug 23 '13 at 03:24
  • What do you mean by "windows mode"? Without a console window? – fernacolo Aug 23 '13 at 03:35
  • I mean not on fullscreen and with a window size defined by parameters. Sorry about my bad english. – Herbert Souza Silva Aug 23 '13 at 04:04
  • Your English is good, don't worry. There's no way to do what you want in Java SE. You need a native Java library that wraps the [CreateProcess API](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx), or an external utility. Take a look a [this question](http://stackoverflow.com/questions/7977322/set-the-window-position-of-an-application-via-command-line). – fernacolo Aug 23 '13 at 06:36
  • By the way, you should consider following StackExchange ethics and mark this answer as a solution. :) – fernacolo Aug 23 '13 at 06:39
  • Alright. Thank you and everyone else for helping. I will take a look at the link you sent me. – Herbert Souza Silva Aug 23 '13 at 22:42