getenv() not working

4.6k views Asked by At

I've created a stand alone a java application in Ubuntu 10.04 using Netbeans 6.9. I'm not able to use use the getenv() command in Netbeans, though if i create a separate java file in gedit and compile it in the terminal then it gives the desired output.

System.out.println(System.getenv("TRGRAPH"));

The above code when executed through the terminal gives the desired output but the same code if i try to run in Netbeans then it returns a null string.
Can anyone tell me how to get the output using netbeans??

3

There are 3 answers

0
Bert F On

I use Eclipse, not NetBeans, but I bet they are similar. Look for a dialog that controls how your program gets launched. This dialog probably has a place where you can specify environment variables that should be set when your app is launched.

The other alternative is to set the environment variable before you launch Netbeans.

3
dogbane On

You need to launch Netbeans from the same terminal after you have set and exported TRGRAPH.

Example, in a terminal:

$ export TRGRAPH=foo
$ netbeans&
0
maaartinus On

It means that TRGRAPH is not defined in the process. The environment gets inherited from the environment of Netbeans. Make sure, that Netbeans gets the variable, e.g., by starting it from a command line or by invoking it using a shell script sourcing your .bashrc (or wherever you define TRGRAPH).


Alternatively, you can start an external Java process using the ProcessBuilder and pass it any environment you like. Quite complicated, but very flexible.