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\\r untime\\bi n;"});
Is there a way to set the system PATH variable at runtime ?
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.
Is there a way to set the system PATH variable at runtime ?
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.
What is the command, and you executing your command using the command shell, cmd?
You could also specify the absolute path to the app.
Depending on you operating system (Windows 98,2000, etc)it should be something like this:
mRuntime.exec("cmd /c PATH=write something");
mRuntime.exec("cmd /c PATH=write something");
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:\\p oet80\\run time\\bin; ");
It didn't work
mRuntime.exec("cmd /C PATH=c:\\jdk1.3\\jre\\1.3\
It didn't work
> mRuntime.exec("cmd /C PATH=c:\\jdk1.3\\jre\\1.3\ \bin;c:\\p oet80\\run time\\bin; ");
Your not running anything?
What is it you r trying to achieve?
Your not running anything?
What is it you r trying to achieve?
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.
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?
what do u need to set the path for?
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!
ASKER
It would be great if I can get the code.....
Thanks in advance
Thanks in advance
SysUtils.java
public class SysUtils
{
static
{
System.loadLibrary("SysUti ls");
}
public native static String getEnvironmentVariable(Str ing name);
public native static void setEnvironmentVariable(Str ing 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->GetStringUTFChar s(f,0);
const char * parameters=env->GetStringU TFChars(p, 0);
const char * directory=env->GetStringUT FChars(d,0 );
DWORD res=(DWORD)ShellExecute(NU LL,"open", file,param eters,dire ctory,SW_S HOWNORMAL) ;
if(res<=32)
{
char buffer[256];
FormatMessage(FORMAT_MESSA GE_FROM_SY STEM,NULL, GetLastErr or(),NULL, buffer,256 ,NULL);
printf("%s\r\n",buffer);
}
env->ReleaseStringUTFChars (f,file);
env->ReleaseStringUTFChars (p,paramet ers);
env->ReleaseStringUTFChars (d,directo ry);
}
JNIEXPORT jstring JNICALL Java_SysUtils_getEnvironme ntVariable (JNIEnv * env, jclass c, jstring n)
{
char value[4096]={0};
jstring v;
const char * name=env->GetStringUTFChar s(n,0);
GetEnvironmentVariable(nam e,value,40 96);
v=env->NewStringUTF(value) ;
env->ReleaseStringUTFChars (n,name);
return v;
}
JNIEXPORT void JNICALL Java_SysUtils_setEnvironme ntVariable (JNIEnv * env, jclass c, jstring n, jstring v)
{
const char * name=env->GetStringUTFChar s(n,0);
const char * value=env->GetStringUTFCha rs(v,0);
SetEnvironmentVariable(nam e,value);
env->ReleaseStringUTFChars (n,name);
env->ReleaseStringUTFChars (v,value);
}
Test.java
public class Test
{
public static void main(String[] args)
{
SysUtils.shellExecute("jav a","com.my classes.Se rver","E:\ \JavaProje cts\\Serve r\\");
}
}
You should specify into the third parameter the directory where yours dll file resides!
This works!
public class SysUtils
{
static
{
System.loadLibrary("SysUti
}
public native static String getEnvironmentVariable(Str
public native static void setEnvironmentVariable(Str
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
{
const char * file=env->GetStringUTFChar
const char * parameters=env->GetStringU
const char * directory=env->GetStringUT
DWORD res=(DWORD)ShellExecute(NU
if(res<=32)
{
char buffer[256];
FormatMessage(FORMAT_MESSA
printf("%s\r\n",buffer);
}
env->ReleaseStringUTFChars
env->ReleaseStringUTFChars
env->ReleaseStringUTFChars
}
JNIEXPORT jstring JNICALL Java_SysUtils_getEnvironme
{
char value[4096]={0};
jstring v;
const char * name=env->GetStringUTFChar
GetEnvironmentVariable(nam
v=env->NewStringUTF(value)
env->ReleaseStringUTFChars
return v;
}
JNIEXPORT void JNICALL Java_SysUtils_setEnvironme
{
const char * name=env->GetStringUTFChar
const char * value=env->GetStringUTFCha
SetEnvironmentVariable(nam
env->ReleaseStringUTFChars
env->ReleaseStringUTFChars
}
Test.java
public class Test
{
public static void main(String[] args)
{
SysUtils.shellExecute("jav
}
}
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?
Why aren't you just setting the environent using the envp parameter of exec?
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\\r untime\\bi n;"});
This is how I pass the environment variable PATH
mProcess = mRuntime.exec(command, new String[] {"PATH=c:\\jdk1.3\\jre\\1.
what is the value of command?
ASKER
The command value is command
"java.exe -classpath c:\poet80runtime\lib\FastO bjects_7JC _Runtime.j ar;c:\vbro ker\vbjava \4.5.1lib\ vbjorb.jar ;c:\jre\1. 3\lib\i18n .jar MyTestApp"
"java.exe -classpath c:\poet80runtime\lib\FastO
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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
Create a batch file and execute that.