I need to run two lines in Runtime.getRuntime().exec(), this two:
cd %CMS_HOME%
ant deploy
Now is it possible to make a .bat file, but I think it is useless for two lines,
is muss be easier! Someone any idea?
Invoke the task programmatically from Java:
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
Put it in a .bat file. It's not useless; that's just how Runtime.exec works.
You should look into using the ProcessBuilder class instead of Runtime.exec(). It was introduced in JDK 5 as the successor to Runtime.exec().
You can use Runtime.exec(String command, String[] envp, File dir) to execute ant deploy in a dir folder.
P.S. Executing ant, which is a java program, using a batch file from another java program is a little bit weird. You can run it just as a java class...
When you are running external application you have to behavior according to the rules of target operating system.
In your case you want to run 2 commands on windows, so you have to say:
cd TheDir && ant
Try it first in command line. Then to make it working run this command with prefix cmd from java:
cmd /c cd TheDir && ant
Alternatively you can use pure java solution. Use ProcessBuilder instead of Runtime.exec(). ProcessBuilder allows you to set working directory, so then you can run ant directly.
And the last point. Actually you do not have to run external process at all. Ant is java application. You can run its main() method directly from you application and specify all needed parameters.
Related
Whenever I need to open the logisim-evolution.jar file, I need to type the command:
java -jar logisim-evolution.jar
I would like to add an entry for that program in /bin to be able to open it through the dmenu. How can I do that?
You're better off making a .sh launcher that runs java -jar logisim-evolution.jar and put it in /bin
#Federico klez Culloca
I want to open an EXE file from a Java program. I tried 2 procedures.
The program can run some programs, like NotePad++, but cannot run my C++ EXE file. I tried:
Process exec = Runtime.getRuntime().exec(file.getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
but neither of the above work. No exception is thrown, and exec.isAlive = true.
Your mistake is that you took the absolute path in the first procedure.
Try using a relative path, I just tested and it worked just fine.
This also works for me (using 2 backslashs').
Runtime.getRuntime().exec("C:\\Program Files\\DDNet\\DDNet.exe");
Kind regards
Was reading this question: Execute .jar file from a Java program and the answer tells you to use a ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
How do I find out the /path/to/java using code? Of course I could look up java in my machine, but an end-user may not necessarily have Java installed in the same place as I do...
Use System.getProperty("java.home").
I want to execute mvn exec:java command from a java program.
I tried Runtime and ProcessBuilder API but unbale to acheive.
Please help me in this problem.
Regards,
Praveen
Most probably java can not find "mvn" executable. Maintaing PATH variable and finding executables is a function of a command line shell (cmd, bash, etc..). Runtime.exec() does not use your shell.
You need to supply full path to executable for Runtime.exec() to find it.
Is there a way to make Java spawn a bash script on Windows? I have cygwin installed, and I've associated the .sh file extension with cygwin bash. The following code, which works on Linux, isn't working:
String[] cmdArray = { "scriptName.sh", "-force", categoryName};
Process proc = Runtime.getRuntime().exec(cmdArray, null, directory);
Try adding either bash or start as the first parameter.
I think you'll have to provide the executable itself in order to do this. The Process.exec method only seems to execute exe files.