1

I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -

@echo off

set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*

@echo on

It is running successfully from command prompt with parameters.

Now I would like to run it from another JAVA program. Please suggest.

Thanks!

1
  • cmd /c test.bat. batch files are not executable, so you execute a cmd shell and tell it to run the batch file. Commented Dec 24, 2015 at 16:01

1 Answer 1

1

I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.

Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");
Sign up to request clarification or add additional context in comments.

3 Comments

How can I pass parameters?
You can simply do p.getOutputStream().write("mybatch.bat 1 2 3\n");. Here I have passed 3 parameters.
p.getOutputStream().write() is not accepting a String as parameter. I tried by converting them to bytes but it is not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.