emin
emin

Reputation: 752

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 ?

Upvotes: 2

Views: 4834

Answers (3)

Alderath
Alderath

Reputation: 3859

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.

Upvotes: 0

Eli Acherkan
Eli Acherkan

Reputation: 6411

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499790

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...

Upvotes: 4

Related Questions