• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use environment variable in CLASSPATH?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there a way to use Java environment variable in classpath in the following manner?

java -Dtest.path="D:/testing" -cp ${test.path}/lib/test.jar

I understand we can use already declared system environment variables like Path in classpath. But I'm looking for the environment variable used by "-D" option.
Kindly let me know if this is possible.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What operating system? Windows?

You could do something like this:

set TEST_PATH=D:\testing
java -Dtest.path=%TEST_PATH% -cp %TEST_PATH%\lib\test.jar
 
Praveen Kumar Jayaram
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the responses.

Actually, in my scenario I've these (classpath, java options) in a configuration file. Sorry for the confusion.
My question was, is there a way to use the environment variable provided using '-D' option to java process in other option like '-cp' for classpath?

Sample configuration:

# A env var declared here
JAVA_OPTIONS=-Dtest.path="D:/Test"

# Above created env var is used in classpath
CLASSPATH=$test.path/lib/test.jar

Assumption is another process or executable reads the configuration and launches JVM by passing the Java options & classpath configured above.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar Jayaram wrote:Thanks for all the responses.

Actually, in my scenario I've these (classpath, java options) in a configuration file. Sorry for the confusion.
My question was, is there a way to use the environment variable provided using '-D' option to java process in other option like '-cp' for classpath?



What you provide with -D are not environment variables. They're system Properties.

Sample configuration:


JAVA_OPTIONS=-Dtest.path="D:/Test"
CLASSPATH=$test.path/lib/test.jar

Assumption is another process or executable reads the configuration and launches JVM by passing the Java options & classpath configured above.



So you want to know if it's possible for some other process (A shell script? A batch file? Some other .exe? A different Java process? Perl script?) to read the above lines from a file and pass them on the command line to an invocation of the java executable?

If so, yes, it's possible.

However, I'm afraid your question is still not very clear. At least not to me.
 
Praveen Kumar Jayaram
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting me. You got it right!

I've a 3rd party executable which picks up the java options and classpath and launches JVM by passing those configured parameters.
So am looking for a standard approach supported by Java to use the system property defined using "-D" option, in classpath (using "-cp" option).
Is there any standard approach?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar Jayaram wrote:
So am looking for a standard approach supported by Java to use the system property defined using "-D" option, in classpath (using "-cp" option).
Is there any standard approach?



Huh? I have no idea what you mean. Are you saying that if your Java app is invoked as java -Dabc=xyz -cp pqr MyApp, then you want your app to use "xyz" as its classpath? If that's what you want, it's not possible exactly as stated. You would have to either

A) Have your app use ProcessBuilder or Runtime.exec() to launch another JVM after getting the "abc" system property to pass to that command line.

or

B) Have your app create a ClassLoader that uses the value of the "abc" system property to find its classes, and use that throughout your app.

A better approach, however, would be to just have the "xyz" in the proper place in the command line to start with.
 
Praveen Kumar Jayaram
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification and the alternative approaches.
As I mentioned, the JVM would be launched by a 3rd party executable and hence I can't play around with the way how the JVM is launched.

Looks this is not achievable the way I expected. Marking the thread as closed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic