简体   繁体   中英

Giving environment variables as arguments

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM