简体   繁体   中英

Proper Java path when using Ubuntu's Runtime.exec()

How do I give the path for Ubuntu's runtime.exec? I am not getting output while executing the below program:

import java.io.IOException;

public class Modify {

    public static void main(String args[]) {
        Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("java -jar home/srinathm/srinath/invalidxml/atlassian-xml-cleaner-0.1.jar home/srinathm/srinath/invalidxml/FAR4031_V_rdf.xml >home/srinathm/srinath/invalidxml/data-clean.xml");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

With his code, data-clean.xml is not generated.

Any help would be appreciated.

My guess is that you don't have Java in your PATH. It is difficult to tell without knowing what the actual error message is.

Try using /usr/bin/java and see if that helps, as shown below:

rt.exec("/usr/bin/java -jar home/srinathm/srinath/invalidxml/atlassian-xml-cleaner-0.1.jar home/srinathm/srinath/invalidxml/FAR4031_V_rdf.xml >home/srinathm/srinath/invalidxml/data-clean.xml");

public static void main(String args[]) {

    try {
        ProcessBuilder builder = new ProcessBuilder(new String[]{"java","-jar","/atlassian-xml-cleaner-0.1.jar","/home/srinathm/invalid xml/FAR4031_V_rdf.xml"});
            builder.redirectErrorStream(true);
            Process p = builder.start();

            BufferedInputStream in = null;
            FileOutputStream out = null;

            try {
               in = new BufferedInputStream(p.getInputStream());
               out = new FileOutputStream("/home/srinathm/srinath/invalidxml/output.xml");

               int c;
               while ((c = in.read()) != -1) {
                  out.write(c);
               }
            }finally {
               if (in != null) {
                  in.close();
               }
               if (out != null) {
                  out.close();
               }
            }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I am getting by using streams

Java's external process execution tools are not a shell, and they don't understand redirection operators like > . Instead of Runtime.exec I would recommend you switch to ProcessBuilder , which can do the redirect to a file directly:

ProcessBuilder pb = new ProcessBuilder("java", "-jar",
  "home/srinathm/srinath/invalidxml/atlassian-xml-cleaner-0.1.jar",
  "home/srinathm/srinath/invalidxml/FAR4031_V_rdf.xml");
pb.redirectOutput(new File("home/srinathm/srinath/invalidxml/data-clean.xml"));
pb.redirectError(ProcessBuilder.Redirect.INHERIT); // send process stderr to java's stderr
Process p = pb.start();
p.waitFor();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM