1

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)
4
  • Kindly post the complete stack trace of the exception. Commented Aug 8, 2018 at 10:10
  • " getting IOException error" - it would be helpful to let us know which error you get (e.g. message, stacktrace etc.) Commented Aug 8, 2018 at 10:14
  • @Azeem I have added the exception in EDIT Commented Aug 8, 2018 at 10:22
  • @Thomas I have added in Edit , please have a look Commented Aug 8, 2018 at 10:22

2 Answers 2

3

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");

P.S. 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.

Sign up to request clarification or add additional context in comments.

10 Comments

yes , that's correct , I am doing some mistake on the command . That's what my question is how can I write the command, so that I can execute it in java 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 rather than doing separately in batch .
Your first point is making sense. Since I don't know much about batch scripting , how will I achieve this in my case.
What exactly is dita in your command? A script file name?
If so, you can just directly use exec to execute it directly. If not, then what's the error?
I have to be in this directory C:\xyz-3.1.1\bin> to execute the command.
|
2

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.

12 Comments

Its throwing error java.io.IOException: CreateProcess error=267, The directory name is invalid
@WhoAmI check you're using the correct directory name. If you can't see the problem, show me the exact call you make to directory(...).
Yes I am sure I am using the same directory call. I can do it successfully manually by command prompt.
Well, that error message means that the directory name you supplied was not a valid directory. Perhaps you used the name of a regular file instead of a directory, or the name contained an illegal character such as ">".
By the way, which version of the JDK are you using? I'm using javac 1.8.0_181 (64-bit) and it works, but I think earlier versions couldn't directly execute batch files but instead you had to run the Command Processor (cmd.exe) to run the batch file. So you might try "cmd.exe", "/c", "dita", "--input=..." etc.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.