Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Set env var Java

Options
  • 28-02-2003 1:42pm
    #1
    Closed Accounts Posts: 536 ✭✭✭


    I have a servlet on Solaris using JNI and I need to set the LD_LIBRARY_PATH for it to work.

    What I want to do is set that variable from the servlet on startup.

    I already set the java.library.path using :
    System.getProperties().put("java.library.path","/usr/lib");

    I've tried to use the same syntax to set the LD_LIBRARY_PATH :
    System.getProperties().put("LD_LIBRARY_PATH", "/usr/lib");
    which doesn't work.

    I reckon I need to be able to export it as well which is where I'm having the problem.
    From the command Line I do the following :
    LD_LIBRARY_PATH=/usr/lib
    export LD_LIBRARY_PATH

    I've tried to do it this way :
    Runtime.getRuntime().exec(new String [] {"export" , "LD_LIBRARY_PATH=/usr/lib"});

    which gives me the following error :
    java.io.IOException: export: not found
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:54)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:550)
    at java.lang.Runtime.exec(Runtime.java:475)
    at java.lang.Runtime.exec(Runtime.java:441)

    I've used the same syntax for deleting a folder before ( e.g. rm -rf folder)

    grrrrr:(


Comments

  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Why don't you set it before you start the servlet engine ?

    I mean, why do you specifically neded to set it from the Java Code ?


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by deadl0ck
    Why don't you set it before you start the servlet engine ?

    I mean, why do you specifically neded to set it from the Java Code ?


    I do that at the moment. But we ship the servlet to customers and I'm trying to make it as user friendly as possible.


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    I don't think you need to go to that level of detail.

    What you could do is get the current location that LD_LIBRARY_PATH points to and then look for a specific library (using File.exists()), that you know you need. If the library does not exist on any of the paths, the print an error message and exit. That way you can tell the user to set the library path - and you're not touching the enviornment


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by deadl0ck
    I don't think you need to go to that level of detail.

    What you could do is get the current location that LD_LIBRARY_PATH points to and then look for a specific library (using File.exists()), that you know you need. If the library does not exist on any of the paths, the print an error message and exit. That way you can tell the user to set the library path - and you're not touching the enviornment


    Ah but I want to ;)
    The servlet is a background process. The customer should not have to so anything to configure it other than starting the server , tomcat in my case. There is no frontend to the servlet so I have nowhere to print out the error message, other than the log files.

    I have a client application that connects to the server, that's the frontend. and I can't tell from there if the LD_LIBRARY_PATH is set or not.

    Plus I'm using JNI remember so I install the library to '/usr/lib' .
    So what I want to do is append the LD_LIBRARY_PATH.


  • Closed Accounts Posts: 286 ✭✭Kev


    export is a bash shell command not an executable program.


  • Advertisement
  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by Kev
    export is a bash shell command not an executable program.

    Yeah I know , rm works if I do the following though:

    Process ps = Runtime.getRuntime().exec(new String [] {"rm" , "-rf", ""+path+"data"});


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    If it's the JNI libs you need, how do you know the customer will even have them ?

    Shouldn't this be a pre-install step by the customer, to make sure he has the JNI stuff and sets LD_LIBRARY_PATH ?

    I mean, it's a servlet on Tomcat, so will the JNI stuff be even guaranteed to be there ? Does it come with Tomcat ?
    If so, you shouldn't have to export the LD_LIBRARY_PATH. If it doesn't come with tomcat then the customer needs JRE (or JDK) in the first place, so when they install that they should set the LD_LIBRARY_PATH

    That being said, I still don't now how you do it .....


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by deadl0ck
    If it's the JNI libs you need, how do you know the customer will even have them ?

    That being said, I still don't now how you do it .....

    I use Installanywhere to create my installation package.
    I initiate the installation of the JDK followed by Tomcat followed by my servlet and my library.
    It's as user friendly as I can get it.
    The only thing that's missing is to automate setting the LD_LIBRARY_PATH to point to the library I install.

    The library I'm referring to is my own library not a system library.


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Why not put the library file in the same location as the class that uses it.

    ./ is ususally on the LD_LIBRARY_PATH, any anyway, it should look in the same directory as where that class that loads it is...


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by deadl0ck
    Why not put the library file in the same location as the class that uses it.

    ./ is ususally on the LD_LIBRARY_PATH, any anyway, it should look in the same directory as where that class that loads it is...
    Well for one thing I can't guarantee that the LD_LIBRARY_PATH will be set. I'm presuming that the library path is to do with having an development environment. Which the customer isn't guaranteed to have.

    I have never seen the LD_LIBRARY_PATH set on Solaris, I've always had to set it myself. And I have a development environment on my Solaris.


  • Advertisement
  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Would it be possible to write a shell script to set the LD_LIBRARY_PATH, and then call that shell script with exec() ?


  • Closed Accounts Posts: 286 ✭✭Kev


    the environment variable would only exist for the shell that is run and any child processes that it spawns, so as soon as it exits the variable would disappear.

    if your java program is executed from a shell script you could set the variable there, or for any exec() calls you make in java you can pass in any environment variables you want but i don't know any way of setting environment variables for jni.


  • Registered Users Posts: 1,186 ✭✭✭davej


    AFAIK there is no way for a java application to change it's own environment variables. This is a design decision by Sun. When you execute your servlet, it inherits the environment of the parent process. Even if you could get the export command to work using Runtime.getRuntime().exec (which by the way is possible) it would simply start a new child process and then terminate it again.

    I suggest you wrap your servlet startup inside a script that sets the env variable first.

    davej


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by davej
    I suggest you wrap your servlet startup inside a script that sets the env variable first.

    davej

    Righteo that would solve my problems :D

    thanks


  • Closed Accounts Posts: 286 ✭✭Kev


    Originally posted by davej
    Even if you could get the export command to work using Runtime.getRuntime().exec (which by the way is possible)

    how is it possible ?


  • Registered Users Posts: 1,186 ✭✭✭davej


    how is it possible ?

    You need to use the exec (String [] cmdarray) method instead of the exec(String command) method.

    This allows you to execute a command (first element of array) along with it's arguments (subsequent elements).

    If you look at man bash you will see that there is a "-c string" option. Thus you can create a String array with 3 elements: "bash", "-c", "built-in-Command blah" and pass this to the exec method.


    davej


  • Closed Accounts Posts: 286 ✭✭Kev


    but that won't have any effect so its not the same thing.


Advertisement