In my Java application, I want to run a batch file that calls 'scons -Q implicit-deps-changed buildfileloadtype exportfileloadtype'It seems that I can't even get my batch file to execute. I'm out of ideas. This is what I have in Java. Mar 31, 2015 I am trying run a batch file from my java codes, but unfortunately I could not run it properly. Actually the batch file is running but only the first line of the batch file is running, so please give solution to it, here are the codes and the batch file.
Active4 years, 2 months ago
I need to run exe from my java application.So I wrote a .bat file and I call it from my java app.Batch file runs the exe.
Here it is:
But there can be different ways to call exe from java.For example.
Case:1
Case:2There is no cmd here.Direct link to .bat file.
Case:3And here there is no .bat file and cmd.Directly run the exe.
So which way is better.And my real question is that is it necessary to destroy the process.
I use waitfor method and it returns 0 if the code works well.
Question 1:What is the correct way to kill the process?
Question 2:Is it necessary to kill process.If I don't kill what happens?
Question 3:If I use .bat file need I kill the process?
Question 4:If I don't use .bat,cmd and directly run the exe is it necessary to kill the process?
And lastly what is difference between waitfor and destroy methods?Is waitfor method also destroy the process?
javauser35javauser3533711 gold badge44 silver badges1717 bronze badges
1 Answer
In your case the best solution would be to simply call your executable by using ProcessBuilder with .exe path as the command.
Regarding your questions (according to java 7 doc):
destroy
kills the process, as the name itself explains.- In this case it's not needed. Since when the program ends, it's automatically destroyed. But it's not a bad practice to do that (same with scanner and input streams, etcetera).
- Again, it's not necessary, but it's a good practice, but it's better to just call the .exe directly.
- The same.
Difference between WaitFor and Destroy:WaitFor basically makes the thread which executes the program to wait until it finishes whereas destroy finishes the process.
Fernando Martín BesteiroFernando Martín Besteiro
Not the answer you're looking for? Browse other questions tagged javabatch-fileprocessprocessbuilder or ask your own question.
Active6 years, 10 months ago
I have multiple .java files inside a folder (e.g temp/code/project)I want a batch file that will compile and run these java files.The batch file should create class files inside the same structure where java files are located starting with the classes folder (i.e classes/temp/code/project)
I don't know how to write a batch file. Can any body help me with this? Thanks in advance.
Paul Whelan11.9k1111 gold badges3939 silver badges7272 bronze badges
Wasim WaniWasim Wani30322 gold badges77 silver badges2222 bronze badges
2 Answers
Theoretically you should write the following:
If you have one class that depends on all other classes in your project it is enough to run javac with this class only: compiler will compile everything.
This is only example and a good exercise. In real life people use special build tools like good old Ant, Maven or newer Graidle or Buildr. I'd recommend you to take one of them.
djangofan16.6k4949 gold badges152152 silver badges243243 bronze badges
AlexRAlexR102k1212 gold badges105105 silver badges183183 bronze badges
Something along the lines of
You may not need to include the -classpath tag (I always do so that I don't have to worry about maintaining my CLASSPATH variable. The -d tag specifies a particular directory to place generated class files.
jezjez51411 gold badge33 silver badges1010 bronze badges