I'm trying to pass a command like the following to Runtime.getRuntime().exec() where the option should be at the beginning.
option="something" ./test.sh a b
Whatever I'm trying, I get this exception:
Cannot run program "option="something"": error=2, No such file or directory
Any ideas?
Doing option="something" only works in shells to set an environment variable for this specific process. To do this in java you must use one of the overloads of exec that take environment variables as a parameter.
Such as:
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[])
or use ProcessBuilder:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Edit:
You should note that explicitly setting environment variables for the new process, causes all environment variables in the current program to be ignored. If you want to pass the current environment variables you must include them yourself.
Related
I need to pass properties to a java execution without using the -D syntax, is it possible?
Normally I run:
java -DrandomProperty=randomVal MyPackage.MyClass
But now, I cannot pass properties to the java command (I have no access to the actual call because it's nested) and I can neither pass them as environment variables (I do not have access to MyPackage.MyClass sources and thus I can't replace System.getProperty("randomProperty") calls with System.getenv("randomProperty").
One way is to set the environmental variable _JAVA_OPTIONS. The JVM reads this environmental variable to get the default JVM parameters to start with. Setting the _JAVA_OPTIONS to -DrandomProperty=randomVal will cause all JVMs to start as though the flag was passed into the command line.
http://progexc.blogspot.co.uk/2013/12/what-i-discovered-while-trying-to-pass.html
This question already has answers here:
Environment variables in Mac OS X
(10 answers)
Closed 7 years ago.
I am trying to System.getenv() to get the value of an environment variable that I have set through my terminal (Mac), I also set the variable in my .bash_profile file and reloaded. After doing so, I echo'd the value and the correct value was printed to the terminal. When trying to retrieve the value of the variable (I made sure I was using the correct name in both my .bash_profile file and when using System.getenv().
In the below code, I have replaced the name of the variable with VAR_NAME:
String varValue = System.getenv("VAR_NAME");
System.out.println("Value: " + varValue);
In my .bash_profile:
export VAR_NAME="foo"
"null" is printed when I print out the value of varValue.
What could be the cause of this?
Edit: I followed the top answer here, restarted Eclipse and it worked!
The answer to this question is more general than just System.getenv() in Java.
Every process has its own independent copy of environment variables, while the environment variables only go down the process tree and are copied from parent to child only when the child process is created. In your case, your shell, which itself is a process, started/created the Eclipse process. Therefore, Eclipse is a child process of your shell and therefore, the environment variables defined on your Eclipse instance are a copy of those that had been defined on your shell when you launched Eclipse.
You probably defined the environment variable on your shell after you had launched Eclipse. Hence, Eclipse and the child Java processes it created, would never "know" about your new environment variable.
Due to this behavior, actually the solution here is to exit Eclipse and launch it again from your shell, in which the environment variable is already defined. Another option is to go to the run configuration of the project and define there the environment variable.
P.S.
Obviously, if you restart your computer, the environment variables you have defined on your shell will not be saved, simply since the shell process you defined the variables on will be gone.
If you use bash, then by adding the environment variable setting command to the file ~/.bashrc, which is executed each time a bash process is started, you can simulate the behavior of permanent environment variables.
There are additional ways to define permanent environment variables. You can take a look here for more information.
Using Java (JSch API) I am trying to execute an Unix command on a remote machine. Now, to successfully execute this command I need to use all the environment variables already set on the remote box.
I can use export <variable> command to set the variables on runtime. But as the number of such variables is quite large I am wondering if there is any better way to use the variables in runtime.
Can anyone help please or should I explain a bit more?
You can save variables into a file:
set > /tmp/vars
echo "A=120" >> /tmp/vars
and then "import" the variables with dot in a script like this:
set -a
. /tmp/vars
mycommand
The work around I found was to use
set | mycommand
Set will manually call the initialization process and it will add environment variables into the scope.
JSch calls bash with -c. This causes .bashrc not to be initialized inside of the bash scope. Set will read the .bashrc file and other config files.
I have a binary executable (no source code) which requires this command to be executed in the terminal - export LD_LIBRARY_PATH = "" to link to a library lbtiff.so.3 which I have in a directory. Only after I execute this export command, I can execute the binary, otherwise it gives an error - "error while loading shared libraries: libtiff.so.3...
Now, I want to execute this binary from my java code. But simply executing the export command in the runtime does not do anything and the "error while .." error still occurs when I execute the binary from Java. I guess setting the unix specific environment variable LD_LIBRARY_PATH might not be possible from Java - is there a way I can run my binary from Java and it is able to find the libraries? Here's my current code -
Process p = Runtime.getRuntime().exec("bash -c export LD_LIBRARY_PATH=<lib path>");
p = Runtime.getRuntime().exec("<binary path>");
Rather than Runtime.exec, use ProcessBuilder. That will allow you to specify environment variables when you run the binary that requires them
ProcessBuilder pb = new ProcessBuilder("<binarypath>");
pb.environment().put("LD_LIBRARY_PATH", "<libPath>");
Process p = pb.start();
Your approach with two separate Runtime.exec calls will not work, because the environment settings you make in the first one only affect that particular Process, not subsequent processes started by a separate invocation of Runtime.exec.
See my answer to another question. The best way is to not use an external shell to set the environment variable (your code doesn't work because it will not set the variable globally, only for the bash process), but to set the variable from within Java. Much easier and it works (and on all platforms, regardless of which shell is installed).
On unix systems you can prepend the variable before executing the command
LD_LIBRARY_PATH=... foo args
Will execute the program foo with args using the modified LD_LIBRARY_PATH
Or you could take advantage of the subshell by using:
(export LD_LIBRARY_PATH=...; foo args)
I have a jar file that gets arguments from commandline and I want to give parameter that contains environment variable. Something like below:
java -jar MyDev.jar -f %PROJECT_HOME%/src/test
But in above case program creates a directory named %PROJECT_HOME% however I want that PROJECT_HOME value in system is /home/jack path. And program should follow /home/jack/src/test not %PROJECT_HOME%/src/test path.
How can I do that ?
Are you running this in a Unix shell? If so, I suspect you just want:
java -jar MyDev.jar -f ${PROJECT_HOME}/src/test
Using % is the Windows way of specifying environment variables - which doesn't appear to fit with a home directory of /home/jack...
The component responsible for environment variables substitution is the shell/command line processor (cmd.exe on Windows).
I wrote the following main method:
public static void main(String[] args) {
System.out.println(args[0]);
}
When I pass "%PATH%" as an argument, running it from within Eclipse prints out %PATH%. Running it from the command line prints out the actual path environment variable.
Note that you can access environment variables from your Java code by using System.getenv().
For example, System.out.println(System.getenv("PATH")) prints out the actual path variable both from Eclipse and from the command line.
One very likely cause for this could be that the variable PROJECT_HOME is not defined or has a misspelled name. Hence, unless you have already done so, you should do echo %PROJECT_HOME% right before you start the java program in order to ensure that the variable is defined.