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 ?
cavitsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Create a batch file and execute that.
0
objectsCommented:
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.
0
msterjevCommented:
Depending on you operating system (Windows 98,2000, etc)it should be something like this:

mRuntime.exec("cmd /c PATH=write something");
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

cavitsAuthor Commented:
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
0
objectsCommented:
> 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?
0
cavitsAuthor Commented:
I am just trying to set the path variable in my main() mehtod so that the current JVM recognises this path for further execution
0
objectsCommented:
you cannot do that.
The exec runs in a new command shell.
0
objectsCommented:
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?
0
cavitsAuthor Commented:
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.
0
msterjevCommented:
I can write for you the JNI wrapper for WIN32 ShellExecute or CreateProcess api!
0
cavitsAuthor Commented:
It would be great if I can get the code.....

Thanks in advance
0
msterjevCommented:
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!
0
objectsCommented:
> 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?
0
cavitsAuthor Commented:
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;"});



0
objectsCommented:
what is the value of command?
0
cavitsAuthor Commented:
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"
0
objectsCommented:
try it with:

"cmd /c 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"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
msterjevCommented:
What is wrong with my solution?
0
CleanupPingCommented:
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.
0
TimYatesCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.