简体   繁体   中英

Run batch file code from java code

I want to execute a batch file code from java button click. Also I don't want any command prompt window to be shown all from java code.

I have a code :-

C:\xyz-3.1.1\bin>dita --input=C:/Users/india/Desktop/mobile-phone/m
obilePhone.xyz --format=pdf --output=C:/Users/india/Desktop --logfile=C:/Use
rs/india/Desktop/dofhdif.txt

So I want above code to be run from batch command with C:\\xyz-3.1.1\\bin> as the parent directory.

Also I want to update --input file path whenever I will choose new file from JFileChooser.

I did this from the java code on button click transform:-

ProcessBuilder pb=new ProcessBuilder("dita --input=C:/Users/india/Desktop/mobile-phone/mobilePhone.xyz --format=pdf --output=C:/Users/india/Desktop --logfile=C:/Users/india/Desktop/dofhdif.txt");
pb.redirectErrorStream(true);
Process process=pb.start();

and getting IOException error.

I get stuck over here for long time , where am I going wrong.

EDIT :- error

java.io.IOException: Cannot run program "dita --input=C:/Users/india/Desktop/mobile-phone/m
obilePhone.xyz --format=pdf --output=C:/Users/india/Desktop --logfile=C:/Use
rs/india/Desktop/dofhdif.txt": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)

As the Error mentioned, it cannot locate the command because the whole string will be treated as the command by ProcessBuilder .

Try to use Runtime.getRuntime().exec directly, but you have to ensure the command dita can be found.

Process process = Runtime.getRuntime().exec("C:\xyz-3.1.1\bin>dita --input=C:/Users/india/Desktop/mobile-phone/mobilePhone.xyz --format=pdf --output=C:/Users/india/Desktop --logfile=C:/Users/india/Desktop/dofhdif.txt");
process.waitFor();
int exitCode = process.exitValue();
System.out.println(IoHelper.output(process.getInputStream())); // handle the output;

Before JDK 5.0, the only way to start a process and execute it, was to use the exec() method of the java.lang.Runtime class after which ProcessBuilder can be used to help create operating system processes.

The major improvement being that, it also acts as a holder for all those attributes that influence the process. And this is how it should be used:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

PS Actually Runtime.getRuntime().exec can also be used with String... as:

Runtime.getRuntime().exec(new String[]{"curl", "-v", "--cookie", tokenString, urlString});

My personal preference:

  1. If you have to configure the environment for the command: to control the working directory or environment variables and also you want to execute the commands several times, you'd better use it since the ProcessBuilder will hold the settings and what you need to do is just processBuilder.start() to create another process with the same settings;

  2. If you want to execute a whole long string command as you mentioned, you'd better just use Runtime.getRuntime().exec since you can just execute it right there without any bothering of the parameter format.

Try this:

String inputFile = ...;
String outputFile = ...;
String logFile = ...;

ProcessBuilder pb = new ProcessBuilder(
        "dita",
        "--input=" + inputFile,
        "--format=pdf",
        "--output=" + outputFile,
        "--logfile=" + logFile)
    .directory(new File("C:\\xyz-3.1.1\\bin"))
    //.inheritIO();
    .redirectErrorStream(true);

Process process = pb.start();

This shows the following points:

  1. The command is separated from the arguments
  2. The argument values can be determined at runtime
  3. The command's default directory ( C:\\xyz-3.1.1\\bin ) is set before starting the process
  4. Consider using inheritIO() instead of redirectErrorStream() if you want the process's output to appear as part of your Java application's output.

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