aspose file tools*
The moose likes Java in General and the fly likes Passing environment information to native processes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing environment information to native processes" Watch "Passing environment information to native processes" New topic
Author

Passing environment information to native processes

krats ram
Greenhorn

Joined: Oct 11, 2009
Posts: 9
I have some code where I launch a native c executable using Process Builder as follows :



Within the native executable I am just dumping the Path environment variable to an output file.

Now if I run the above program as follows :

java -Djava.library.path=C:\\temp Test.java

I thought when the java program launches a seperate child process, the contents of the java library path would be added to the Path environment variable and would be available for the child process. But I dont see this happening.

Is this the way it is supposed to be?

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

java.library.path has nothing to do with the actual path. You can do this manually though, with the Map returned by pBuilder.environment():


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
krats ram
Greenhorn

Joined: Oct 11, 2009
Posts: 9
Seems like on windows, if you dont explicitly specify -Djava.library.path, it defaults to the contents of the PATH environment variable.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

krats ram wrote:Seems like on windows, if you dont explicitly specify -Djava.library.path, it defaults to the contents of the PATH environment variable.


Yeah, because on Windows the default search path for DLLs is the PATH.

shudder


[Jess in Action][AskingGoodQuestions]
krats ram
Greenhorn

Joined: Oct 11, 2009
Posts: 9
Something seems confusing. I tried the following program



When I compile as javac Test.java and run as java Test , I get the following output

Parent process info :
-----------------------
PATH environment variable from System Properties : null
java.library.path environment variable from System Properties : C:\Java\jdk1.6.0
_04\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Java\jdk1.6.
0_04\bin

Child process info :
-----------------------
PATH environment variable from Child Process Builder environment : C:\Java\jdk1.
6.0_04\bin
java.library.path environment variable from from Child Process Builder environme
nt : null

The above implies that the PATH variable is not available through the System Properties but somehow it gets set for the environment if a child process is created through ProcessBuilder. Also the java.library.path is not automatically transferred from the parent process to the environment of the child process.

Is that the desired behavior?

Thanks

PS Contents of my PATH variable when echoed from DOS is C:\Java\jdk1.6.0_04\bin

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

System properties are not at all the same as environment variables. Environment variables are not copied into system properties, and system properties are not copied into environment variables. Although the default variables of some system properties might come from environment variables on some platforms, this is a platform-specific implementation detail.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Ernest is right. To get environment properties, use System.getenv. These are already made available for ProcessBuilders but system properties are not.
krats ram
Greenhorn

Joined: Oct 11, 2009
Posts: 9
Thanks for the response.

The java documentation mentions that System.getenv is deprecated and System.getProperty should be used

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)

thats why was expecting System.getProperty to return the environment variables.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

They mean to say that you should pass system-dependent arguments to Java programs using "-DFOO=bar" rather than by using an environment variable named FOO.
krats ram
Greenhorn

Joined: Oct 11, 2009
Posts: 9
Thanks for the response.

If the getEnv method returns environment variables but is merely not supposed to be used to extract System properties, I dont know if it should be marked as deprecated (as in the documentation). A comment there mentioning the usage would be more appropriate unless ofcourse it has been moved to a different class or something.

Anyway thanks again for all the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing environment information to native processes