Execute a Java program from our Java program

后端 未结 14 1794
名媛妹妹
名媛妹妹 2020-12-01 17:24

I used

Runtime.getRuntime().exec(\"_____\")

but it throws a IOException as below:

java.io.IOException: Create         


        
相关标签:
14条回答
  • 2020-12-01 17:54
    public class Test {    
      public static void main(String[] args) throws Exception {    
        Process p = Runtime.getRuntime().exec("\"c:/program files/windows/notepad.exe\"");
        p.waitFor();
      }
    
    }
    

    The above works quite well, instead of passing \"c:/program files/windows/notepad.exe\" as the arguments for the executable, use the path to your program, I'm not sure if this solution is JVM version dependent, or if it can use relative paths.

    0 讨论(0)
  • 2020-12-01 17:55
    java.io.IOException: CreateProcess: c:/ error=5
            at java.lang.Win32Process.create(Native Method)
            at java.lang.Win32Process.<init>(Win32Process.java:63)
            at java.lang.Runtime.execInternal(Native Method)
    

    If I recall correctly, error code 5 means access denied. This could be because your path is incorrect (trying to execute "c:/") or you are bumping against your OS security (in which case, look at the permissions).

    If you are having trouble locating the Java executable, you can usually find it using system properties:

    public class LaunchJre {
    
        private static boolean isWindows() {
            String os = System.getProperty("os.name");
            if (os == null) {
                throw new IllegalStateException("os.name");
            }
            os = os.toLowerCase();
            return os.startsWith("windows");
        }
    
        public static File getJreExecutable() throws FileNotFoundException {
            String jreDirectory = System.getProperty("java.home");
            if (jreDirectory == null) {
                throw new IllegalStateException("java.home");
            }
            File exe;
            if (isWindows()) {
                exe = new File(jreDirectory, "bin/java.exe");
            } else {
                exe = new File(jreDirectory, "bin/java");
            }
            if (!exe.isFile()) {
                throw new FileNotFoundException(exe.toString());
            }
            return exe;
        }
    
        public static int launch(List<String> cmdarray) throws IOException,
                InterruptedException {
            byte[] buffer = new byte[1024];
    
            ProcessBuilder processBuilder = new ProcessBuilder(cmdarray);
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            InputStream in = process.getInputStream();
            while (true) {
                int r = in.read(buffer);
                if (r <= 0) {
                    break;
                }
                System.out.write(buffer, 0, r);
            }
            return process.waitFor();
        }
    
        public static void main(String[] args) {
            try {
                Runtime.getRuntime().exec("c:/");
    
                List<String> cmdarray = new ArrayList<String>();
                cmdarray.add(getJreExecutable().toString());
                cmdarray.add("-version");
                int retValue = launch(cmdarray);
                if (retValue != 0) {
                    System.err.println("Error code " + retValue);
                }
                System.out.println("OK");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    (Tested Windows XP, Sun JRE 1.6; Ubuntu 8.04, OpenJDK JRE 1.6)

    This is the equivalent of running:

    java -version
    

    You may also want to look at the "java.library.path" system property (and "path.separator") when trying to locate the executable.

    0 讨论(0)
提交回复
热议问题