Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing environment information to native processes

 
krats ram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.library.path has nothing to do with the actual path. You can do this manually though, with the Map returned by pBuilder.environment():
 
krats ram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
krats ram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic