I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.
Now I want to do the same in Java by the use of:
static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
I know that source is an internal command and I can not call it from Java.
Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?
Thank you in advance!
Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();
Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.
Related
I am trying to execute cmd commands inside a java program using the following code
String command = "clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
This is executing in java without any exceptions, but the actual command is not running. If the command is run it should create text file foodout.txt in the location mentioned. Nothing is happening.
The actual command is
clingo food1.lp fooddata.txt 0 >>foodout.txt
clingo is a windows executable program. This command works fine when run in command prompt. I want to run this inside java program from click of a button. I have set environment variable for clingo. Clingo and this java project are in the same directory.
Before this i tried below code
String[] command = {"clingo", "food1.lp","fooddata.txt", "0", ">>foodout.txt"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File(WorkingDirectoryArea.getText()));
Process process = builder.start();
where Workingdirectoryarea contains the directory location for commands to be run. This code does nothing.
Can someone guide me or provide code sample on how to run the cmd command inside this java program. I am using Netbeans IDE. Thanks.
you said your command works with a command prompt. OK. If you look closely, the command window has a path entry (cmd= echo %PATH%). That's the difference between executing a command in a command window and executing a java process. You have 2 options.
1. Add the path to the process.
2. Add the path to the clingo command (i.e. "f:\path\clingo.exe ...)
Item 1 is especially needed when using dos commands. To add a path environment:
Runtime.getRuntime().exec not finding file in java environment
You are redirecting standard output to a file. This is not part of the command nor a command line parameter. Is the command interpreter that handles this.
You must invoke the command interpreter to run your program like this:
String command = "cmd /c clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
Note the cmd /cpart which invokes the command interpreter to run your command like you would do on a Windows terminal.
On Linux it would be sh -c or whatever shell you like.
EDIT 1
When running the command, clingo.exe must be in your path or it must be in the default directory for the Java interpreter. If not, you should give the full path to the executable, like this:
String command = "cmd /c F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Try to run
F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt 0 >> F:\\clingo\\foodout.txt
at a Windows prompt and see if it works as expected. If it works it also should work when run from a Java program. Please, replace the clingo path with the right one for your environment.
Your command must be like this: java -jar yourExecuteable.jar yourParameter
In your case: java -jar clingo.jar food1.lp fooddata.txt 0 >>foodout.txt
I'm trying to create a java application which starts a new terminal without using java -jar. I tried using several methods, but none of them have worked.
I need this to work on osx, I was able to make it on windwos
You have to call your shell as a program
Runtime runtime = Runtime.getRuntime();
String[] args = { "/bin/sh", "-c", " java -jar myjar.jar" };
final Process process = runtime.exec(args);
To respond to your specific request, this is my answer
1/ Create a shell script
For example call it loadJava.sh:
#!/bin/sh
java -jar path/to/jar/file.jar
2/ Call the shell script with this java code that open a terminal and run the shell script
Correct code for OSX is
Runtime.getRuntime().exec("/usr/bin/open -a Terminal /path/to/the/script");
I am trying to execute make clean command from a Java program.I need to change the directory first and then execute it.Or is there any other method?
I have tried both the ways as stated in the site.
1. using ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("make "," clean");
pb.directory(new File("\\home\\p\\lipta-v1-3"));
pb.start();
Using Runtime
Runtime runtime = Runtime.getRuntime();
String command="make clean";
process=runtime.exec(command,null,new File("/home/p/lipta-v1-3"));
However none of them are working.The other commands such as cp ,rm etc.are working fine.
I am using Netbeans7.0.1 and ubuntu 12.04
I've been playing around with exec and although it opens a terminal with the user being me, it doesn't seem to be able to execute commands I give it =/ code is as follows:
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("xterm -e \"source /home/USER/.bashrc; ~/./myscript.sh\"")
}
catch(Throwable t)
t.printStackTrace();
In the xterm console I get:
xterm: Can't execvp ": No such file or directory
user#user:$
Which is then a terminal waiting to be used, and will not go away until I ctrl-d it.
Not sure what's going on here?...
Thanks.
I don't think you can pass shell stuff into xterm's -e option. It wants a program and optional parameters for that program. source is a shell built-in.
Something you can try doing is just calling your myscript.sh and modifying it so that it sources your bashrc at the top. Or create a new bash script that sources your rc file then runs myscript.sh.
I want to set a path in my Java program with this Windows command (this path contains some DLL files that are used in a native peripheral of my program):
c:\>path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
But this causes an exception when the program runs:
java.io.IOException: Cannot run program "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\": CreateProcess error=2, The system cannot find the file specified
I don't know why I can set the path with no problem in the command prompt but then get an exception thrown in the code.
String path = "C:\\Users\\NetBeansProjects\\IPTV1.7\\3rd_party\\";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("path=%path%;"+ path);
Your command
path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
is not a "real" windows command, but only a variable assignment, interpreted by your shell (cmd.exe), visible only in the same shell session and any commands (other programs) started from there.
When trying to execute this from Java with Runtime.exec(), the windows CreateProcess function tries to find a executable file with this strange name, which obviously does not exist (can't exist, I think), and you get this error.
Even if you could execute this, for example by calling cmd.exe, it would only influence this same cmd.exe process (and any programs started from there, not your own Java process (and programs started from here).
Depending on what you in fact want, you could, for example:
give the path with ProcessBuilder directly to the process which in fact needs it (like Aaron showed)
search by yourself for executable files, if you want this for finding commands in the next exec
put the variable assignment and other commands together in a .BAT or .CMD file and execute this.
When you type this on the command prompt, the cmd program processes it and changes the PATH variable for you. When you try this with Runtime, no cmd process is created and there is no command "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\" on your harddisk (Windows actually tries to find a program with this exact name).
Put the commands in a .BAT or .CMD file. Windows automatically creates a cmd process to execute them for you.
You can use ProcessBuilder in java to spawn a process and control the environment that it gets. So you would use the ProcessBuilder environment method to set the PATH environment variable, then set the relevant command line, then launch. SO something like (untried):
ProcessBuilder b = new ProcessBuilder();
b.environment().put("PATH", whatever);
b.command(whatever);
Process p = b.start();