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.
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
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.
How can I pass java -jar arguments in a batch file ?
Code below doesn't work:
set classpath = c:/users/abc/desktop/project/trunk/.CLASSPATH
java -jar java_file.jar "%classpath%"
( the java jar file needs to take the location of the .CLASSPATH file as the argument ) .
In my main batch file, classpath variable takes the value depending on which project is checked out from the SVN .
Hope my question is clear.
That's because your set command has spaces around the =. Remove them and it works.
What you have there now essentially means that an environment variable classpath<SPACE> is set to the given value which starts with a space. Therefore, the environment variable %classpath% (without the space in the variable name) doesn't exist at all.
I have a caching app in Java and I need to put objects of different size in cache. The problem is that I didn't really know how to count the size of a custom object and I've found the solution - to use the library: http://mvnrepository.com/artifact/com.googlecode.sizeofag/sizeofag/1.0.0.
To run the program using the library I need to specify command-line argument -javaagent. So, how can I do it if I'm using maven???
The program is simple:
protected static Boolean b;
public static void main( String[] args )
{
System.out.println(SizeOfAgent.sizeOf(b));
}
This is the output:
0
Can not access instrumentation environment.
Please check if jar file containing SizeOfAgent class is
specified in the java's "-javaagent" command line argument.
P.S. I know, that such kind of question already exists, but it has no proper answer.
On a Linux/Unix machine the "mvn" command will use a shell variable "MAVEN_OPTS" to pass in options. This is useful if you want to give Maven more memory. In your .profile or .bash_profile put a line like this in:
export MAVEN_OPTS=-javaagent
On windows:
in shell (cmd.exe) type "set MAVEN_OPTS=..."
or
add MAVEN_OPTS to your environment
On NetBeans:
In ~/.netbeans/6.5/, create etc/netbeans.conf. Add your environment variables there, e.g.:
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"
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)