AnimaVivens
AnimaVivens

Reputation: 409

How to call an external application if using exec() defines it as invalid Win32 application in Java?

I'm fiddling with opening external applications from Java source code. I am trying to open a launcher for a game called Runescape, which is found inside C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex. The name of the file inside this directory is RuneScape Launcher.url.

This is the code which demonstrates my progress so far:

public static void main(String[] args) throws IOException, InterruptedException {
    //doesn't work
    Process p = Runtime.getRuntime().exec("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");

   //if Chrome was to be opened, it works, since it is .exe
   // Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

  p.waitFor();
  System.out.println(p.exitValue());
}

The error that gets thrown is:

Exception in thread "main" java.io.IOException: Cannot run program "C:\ProgramData\Microsoft\Windows\Start": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Main.main(Main.java:47)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

Obviously, RuneScape Launcher.url is not a valid Win32 application. How to start such an application?

My research: - this post suggests using ShellExecute, however it is written in another programming language. I couldn't find a similar solution for Java. - this post talks about passing parameters when calling an external application, but that external application is .exe - this page demonstrates calling external applications, but again only .exe

Then, I've tried starting this launcher from cmd manually ... successfully. First, I've located launcher directory: cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex and then called the launcher: "RuneScape Launcher.url". This started the launcher correctly. Why doesn't it start from Java code?

Upvotes: 1

Views: 459

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64969

Try passing the launcher as an argument to cmd.exe:

    Process p = Runtime.getRuntime().exec("cmd.exe", "/c", "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");

Upvotes: 1

Related Questions