0

So I want to open cmd though java with this code

import java.io.IOException;

public class mainer {
    public static void main(String args[]) {
        try {
           Runtime.getRuntime().exec("cmd.exe /c start");
           System.out.println("ok");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

but I get this error when I execute it

java.io.IOException: Cannot run program "cmd.exe": 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 mainer.main(mainer.java:6)
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

and the funny thing is that it worked 2 days ago and now it gives me this strange error

Nick Nikolov
  • 261
  • 3
  • 13
  • 1
    I am using 32 bit OS and 32 bit java....your code executed successfully in my machine... The problem may with OS and java mismatch(32 bit and 64) – Anptk Jan 17 '15 at 10:12

3 Answers3

2

I think it should be:

Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start"});

To learn more about the start, type help start at command prompt.

Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
0

Seems like your PATH variable doesn't have path to cmd.exe in it so better try this.

import java.io.IOException;

public class mainer {
    public static void main(String args[]) {
        try {
           Runtime.getRuntime().exec(new String[]{System.getenv().get("SystemRoot")+"/cmd.exe", "/c", "start"});
           System.out.println("ok");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
shazin
  • 21,379
  • 3
  • 54
  • 71
0

Maybe you try to start 64-bit cmd.exe from 32-bit JRE.

Try to execute C:\Windows\SysWOW64\cmd.exe instead which is 32-bit.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60