File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Set environment variable of UNIX by java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Set environment variable of UNIX by java program" Watch "Set environment variable of UNIX by java program" New topic

Set environment variable of UNIX by java program

Yurong Chen

Joined: Jul 23, 2002
Posts: 18
Hello, I met a problem when I try to set the UNIX environment variable in my java program for calling matlab. I used a package named JMatLink for calling matlab in java. However, before I starting the following environment variable must be set:

I have used the followng sorce code to try that:

However, when I compiled the program, it gave me error message about "cannot find the setenv command".
Could somebody tell me how to set the environment variable in java program?? Thank you in advance!
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
I think there's a whole lot of misunderstandings going on here.
1. It really doesn't make sense to set an environment variable like this after the JVM has started running. LD_LIBRARY_PATH is used by the system loader to decide where to look for loadable native libraries. By the time your Java program is running, this lookup has almost certainly already happened.
2. "setenv" (like "cd") is an example of a command which is not a real executable, instead it is built in to some of the various shells (command interfaces) on Unix. When you run an external command from Java, the commands you give are not passed to command shell, but looked up and executed directly. You can't run "setenv" like that.
3. Even if you do manage to run a "setenv" (by invoking a command shell and passing the "setenv" as command optionm to it, for example), such settings only last for the life of that shell process. Whan called from Java, such processes only last as long as the command takes to execute, then they are ended and all the context information they contain is discarded. So such a "setenv" would have no effect on later code.
4. " `pwd` " is an example of a command shell escape sequence. If you type it in to a running command shell it will execute the command inside the "ticks", and put the resulting output in the command. In the case of the example you give, it would add the value of "pwd" (the name of the current working directory) to the LD_LIBRARY_PATH.
Such escape sequences make no sense when called from a Java program. Note that for this to work, you need to make sure to use "back-ticks" (`), rather than the regular single-quote characters.
5. You never really know what entries there might already be in a path variable like LD_LIBRARY_PATH, so it is almost always wise to
include any old value just in case.
The solution to all of this is to make a small "shell script" file containing something like the following: For the sake of this discussion we'll call the file 'myjava' - note there is no "file type extension".

then type
chmod a+x myjava
which will make it executable. Now you should be able to just type "myjava" and it will set up your environment and run your java program.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Yurong Chen

Joined: Jul 23, 2002
Posts: 18
Dear Frank ,
Thank you very much! I have tried the way you said and it works successfully.
However, I still have one more problem. I should call the JMatLink package in my JSP in tomcat. Everytime I should pass different parameters (different file names) to call my JMatLink package. How can I do that? Should I ask this question in JSP forum?
Thank you very much!
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Everytime I should pass different parameters (different file names) to call my JMatLink package
I don't really understand what you want to do here. Can you show us the methods you need to call in JMatLink, and examples of what and when you want to pass to them?
Yurong Chen

Joined: Jul 23, 2002
Posts: 18
Dear Frank,
In this project, I should design a web server which can allow the users to make use some algorithms.
1. The algorithms are written in Matlab.
2. The users submit the Excel files which contains the input data of the algorithms.
3. I should use Java to finish the project.
4. I have used the jspSmartUpload Package to upload the excel files from the client side.
5. I have used the JMatLink package to call Matlab in java.
6. I should pass the different file which is uploaded from client side to my algorithms which is written in Matlab.
However, my problem is how to pass the different file names to the "shell script" file ( You taught me yesterday) in JSP and execute the "shell script" file in JSP.

Thank you very much!
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
'setenv' is a shell command, not a program. So 'setenv' can never be a process. You can accomplish the same thing by using a syntax (at least in some systems)

Alternatively, make a shell script called '', and put the two lines
into the file. Of course, chmod +x, and you should be in business.
[ July 30, 2002: Message edited by: Michael Zalewski ]
Yurong Chen

Joined: Jul 23, 2002
Posts: 18
Dear Frank and Michael,
Thank you for your kind help!
I have tried to make a "shell script" as followed:

At the same time, I wrote a java program to run this "shell script". The source code is followed:

This program can run sccessfully with the output " Succesfully" which is from the file.

However, when I put this code in my JSP and run it, it always give me the error page and make the Tomcat cannot be located any more. I have tried this for several days and failed again and again. Could you help me to solve the problem? The source code of my jsp is as followed:

Thank you in advance!
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
You still have two exec calls.
The first one:

will start a process, change directories for that process, then exit. Your parent process (the java VM) is still in the same directory.
Then you exec:

which starts another process having nothing to do with "p1". So it runs in whatever directory Tomcat is running in.
How about doing:


Check javadoc for Runtime and find the signature for exec that meets your needs.
I agree. Here's the link:
subject: Set environment variable of UNIX by java program
Similar Threads
Execute command line command with arguments within Web Application
getting variable of c in java
finding out disk space
Problem with JDBC
Ubuntu and vs LD_LIBRARY_PATH and java.library.path