• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Set environment variable of UNIX by java program

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Yurong
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Yurong Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Yurong
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Yurong
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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 'host.sh', and put the two lines
setenv LD_LIBRARYPATH=xxx
host ${LD_LIBRARYPATH}
into the file. Of course, chmod +x host.sh, and you should be in business.
[ July 30, 2002: Message edited by: Michael Zalewski ]
 
Yurong Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Test.java 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!
Yurong
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

or

Check javadoc for Runtime and find the signature for exec that meets your needs.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic