Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-02-26
20
Medium Priority
?
791 Views
Last Modified: 2013-11-18
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 ?
0
Comment
Question by:cavits
  • 8
  • 6
  • 4
  • +2
20 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8031605
> Is there a way to set the system PATH variable at runtime ?

Create a batch file and execute that.
0
 
LVL 92

Expert Comment

by:objects
ID: 8031682
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
 
LVL 3

Expert Comment

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

mRuntime.exec("cmd /c PATH=write something");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cavits
ID: 8031798
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
 
LVL 92

Expert Comment

by:objects
ID: 8031809
> 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
 

Author Comment

by:cavits
ID: 8031825
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
 
LVL 92

Expert Comment

by:objects
ID: 8031836
you cannot do that.
The exec runs in a new command shell.
0
 
LVL 92

Expert Comment

by:objects
ID: 8031869
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
 

Author Comment

by:cavits
ID: 8032019
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8032157
I can write for you the JNI wrapper for WIN32 ShellExecute or CreateProcess api!
0
 

Author Comment

by:cavits
ID: 8032230
It would be great if I can get the code.....

Thanks in advance
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8032454
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
 
LVL 92

Expert Comment

by:objects
ID: 8037065
> 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
 

Author Comment

by:cavits
ID: 8039509
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
 
LVL 92

Expert Comment

by:objects
ID: 8039535
what is the value of command?
0
 

Author Comment

by:cavits
ID: 8039678
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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 8039718
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8040927
What is wrong with my solution?
0
 

Expert Comment

by:CleanupPing
ID: 9059045
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9671432
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question