Execute a Java program from our Java program

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

I used

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

but it throws a IOException as below:

java.io.IOException: Create         


        
14条回答
  •  猫巷女王i
    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 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 cmdarray = new ArrayList();
                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.

提交回复
热议问题