Solved

getProperty?

Posted on 2001-08-29
6
367 Views
Last Modified: 2008-03-17
When I type "set" at the command prompt, I see:
SystemDrive=C:

In a Java program, I do
System.getProperty("SystemDrive");
I get nothing.

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

Thanks.
0
Comment
Question by:samliam
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

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

0
 
LVL 2

Expert Comment

by:Veroland
ID: 6437089
the flag is the -D
0
 

Author Comment

by:samliam
ID: 6437244
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("os.name"); However, this does not work for other environment variables including "SystemDrive".
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 20 total points
ID: 6437266
...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)

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com

0
 

Author Comment

by:samliam
ID: 6438088
Veroland,
I'll post 20 points for you separately.

Thank you both.
0
 

Author Comment

by:samliam
ID: 6438167
btw,

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

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question