Link to home
Start Free TrialLog in
Avatar of cavits
cavits

asked on

Runtime.exec(String cmd, String[] envp ) is not setting the environment variable PATH

Hi There,
I'm using the Runtime.exec method to execute a command which requires the system PATH. This is what I do in my code to set the PATH

mProcess = mRuntime.exec(command, new String[] {"PATH=c:\\jdk1.3\\jre\\1.3\\bin;c:\\poet80\\runtime\\bin;"});

Is there a way to set the system PATH variable at runtime ?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> Is there a way to set the system PATH variable at runtime ?

Create a batch file and execute that.
According to the javadoc what you are doing to set the environment looks about right. What problem are you having?

What is the command, and you executing your command using the command shell, cmd?

You could also specify the absolute path to the app.
Avatar of msterjev
msterjev

Depending on you operating system (Windows 98,2000, etc)it should be something like this:

mRuntime.exec("cmd /c PATH=write something");
Avatar of cavits

ASKER

The operating system is windows 2000. This is what I tried

mRuntime.exec("cmd /C PATH=c:\\jdk1.3\\jre\\1.3\\bin;c:\\poet80\\runtime\\bin;");


It didn't work
> mRuntime.exec("cmd /C PATH=c:\\jdk1.3\\jre\\1.3\\bin;c:\\poet80\\runtime\\bin;");

Your not running anything?

What is it you r trying to achieve?
Avatar of cavits

ASKER

I am just trying to set the path variable in my main() mehtod so that the current JVM recognises this path for further execution
you cannot do that.
The exec runs in a new command shell.
you would need to create a batch file that sets the path, before starting the vm.

what do u need to set the path for?
Avatar of cavits

ASKER

In my application I'm spawning another process to start my server with java.exe. I can pass the classpath as a JVM argument but the process spawned should have some path settings to access some dll files and other processing which is internal to the server. I need to set the path for the server that I spawn.
I can write for you the JNI wrapper for WIN32 ShellExecute or CreateProcess api!
Avatar of cavits

ASKER

It would be great if I can get the code.....

Thanks in advance
SysUtils.java

public class SysUtils
{
     static
     {
          System.loadLibrary("SysUtils");
     }

     public native static String getEnvironmentVariable(String name);
     public native static void setEnvironmentVariable(String name,String value);
     public native static void shellExecute(String file,String parameters,String directory);
}



SysUtils.cpp

#include <windows.h>
#include <shlobj.h>
#include "SysUtils.h"


JNIEXPORT void JNICALL Java_SysUtils_shellExecute(JNIEnv * env, jclass c, jstring f, jstring p, jstring d)
{
     const char * file=env->GetStringUTFChars(f,0);
     const char * parameters=env->GetStringUTFChars(p,0);
     const char * directory=env->GetStringUTFChars(d,0);
     DWORD res=(DWORD)ShellExecute(NULL,"open",file,parameters,directory,SW_SHOWNORMAL);
     if(res<=32)
     {
          char buffer[256];
          FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),NULL,buffer,256,NULL);
          printf("%s\r\n",buffer);
     }
     env->ReleaseStringUTFChars(f,file);
     env->ReleaseStringUTFChars(p,parameters);
     env->ReleaseStringUTFChars(d,directory);
}

JNIEXPORT jstring JNICALL Java_SysUtils_getEnvironmentVariable(JNIEnv * env, jclass c, jstring n)
{
     char value[4096]={0};
     jstring v;
     const char * name=env->GetStringUTFChars(n,0);
     GetEnvironmentVariable(name,value,4096);
     v=env->NewStringUTF(value);
     env->ReleaseStringUTFChars(n,name);
     return v;
}

JNIEXPORT void JNICALL Java_SysUtils_setEnvironmentVariable(JNIEnv * env, jclass c, jstring n, jstring v)
{
     const char * name=env->GetStringUTFChars(n,0);
     const char * value=env->GetStringUTFChars(v,0);
     SetEnvironmentVariable(name,value);
     env->ReleaseStringUTFChars(n,name);
     env->ReleaseStringUTFChars(v,value);
}



Test.java

public class Test
{
     public static void main(String[] args)
     {
          SysUtils.shellExecute("java","com.myclasses.Server","E:\\JavaProjects\\Server\\");
     }
}

You should specify into the third parameter the directory where yours dll file resides!

This works!
> In my application I'm spawning another process to start my server with java.exe.

Why aren't you just setting the environent using the envp parameter of exec?
Avatar of cavits

ASKER

I tried with the method exec(String cmd, String[] envp)
This is how I pass the environment variable PATH

mProcess = mRuntime.exec(command, new String[] {"PATH=c:\\jdk1.3\\jre\\1.3\\bin;c:\\poet80\\runtime\\bin;"});



what is the value of command?
Avatar of cavits

ASKER

The command value is command
"java.exe -classpath c:\poet80runtime\lib\FastObjects_7JC_Runtime.jar;c:\vbroker\vbjava\4.5.1lib\vbjorb.jar;c:\jre\1.3\lib\i18n.jar MyTestApp"
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is wrong with my solution?
cavits:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept objects' comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer