When I type "set" at the command prompt, I see:

In a Java program, I do
I get nothing.

How do I do this? (getting "SystemDrive"?)

Who is Participating?
Igor BazarnyConnect With a Mentor Commented:
...And -D should go before class name, like:
c:\java -DSystemDrive=C: yourapp

In fact, java doesn't let you access environment variables, so you need to take care about passing them to your code yourself. Another option would be use of Runtime.exec():

Runtime r = Runtime.getRuntime();
Process p = Runtime.exec("command /c set");
InputStream envStream = p.getInputStream();
// parse envStream to get all environment variables

This approach is not portable--on Windows NT/2000 command interpreter called cmd, and more differences for unix--different command interpreter, different command to list environment variables. Mac didn't support environment variables at all (I guess FreeBSD-based OS X supports them)

Igor Bazarny,
Brainbench MVP for Java 1

you have to run your java program with the system property flag i.e.
c:\java yourapp -DSystemDrive=C:

the flag is the -D
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

samliamAuthor Commented:
My question is:
How do I get system information such as SystemDrive or any other environment variables I have set in a DOS box?

For example, to get the OS, I can do System.getProperty(""); However, this does not work for other environment variables including "SystemDrive".
samliamAuthor Commented:
I'll post 20 points for you separately.

Thank you both.
samliamAuthor Commented:

what if I already have e.g.,

ARG="..... a very long string"

How do I use the '-D' flag:

java -DARG=%ARG% my_app??

this does not work...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.