Home arrow static arrow Java Programming [Archive] - setting windows environment variables from Java program
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - setting windows environment variables from Java program
This topic has 18 replies on 2 pages.    1 | 2 | Next »

Posts:8
Registered: 7/28/04
setting windows environment variables from Java program  
Jul 30, 2004 6:48 AM



 
Is there any way to set environment variables from Java program in Windows? Any help is appreciated.

Here is my situation:
I need to decrypt an encrypted Oracle user password in a batch file which will be used while running a sql script with sqlplus. I was planning to have bat file which will call a Java program decrypt the password and set it as an env variable in windows which will be available while calling sqlplus.

thanks
 

Posts:192
Registered: 30/05/01
Re: setting windows environment variables from Java program  
Jul 30, 2004 6:56 AM (reply 1 of 18)



 
have you considered running the sqlplus command from within the java program using Runtime.exec()?

Runtime.exec("<path>sqlplus user/pass@sid");
 

Posts:8
Registered: 7/28/04
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:07 AM (reply 2 of 18)



 
Thanks for the reply.
Unfortunately we already have the bat files that are in use in prod. Currently they use plain text userid and password for sqlplus that is read from another text file. This is a new requirement for encrypting/decrypting the password. So I don't have a choice to change all these bat files and have to work around them. For running all these batch jobs we have a simple html user interface where user just clicks on the individual links to start these jobs. I was thinking about adding a call to the java program to setup the env variable before the web page is displayed.

thanks.
 

Posts:192
Registered: 30/05/01
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:21 AM (reply 3 of 18)



 
doh!

System.setProperty(key, value);

see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
 

Posts:192
Registered: 30/05/01
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:29 AM (reply 4 of 18)



 
double doh!

Actually i don't think that will work. I think it will just set a property that is visible within the JVM - not the OS.

what about writing the decrypted strings to the existing text file and reading them in as you do now?
 

Posts:3,055
Registered: 18/06/98
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:34 AM (reply 5 of 18)



 
Runtime.exec has a lot of overloadings. Two of them allows you to specify the environment variables.


exec
public Process exec(String[] cmdarray,
String[] envp,
File dir)
throws IOExceptionExecutes the specified command and arguments in a separate process with the specified environment and working directory.
cmdarray - array containing the command to call and its arguments.
envp - array of strings, each element of which has environment variable settings in format name=value.
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
 

Posts:8
Registered: 7/28/04
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:55 AM (reply 6 of 18)



 
double doh!

Actually i don't think that will work. I think it will
just set a property that is visible within the JVM -
not the OS.

Yes, System.setProperty will not for me.


what about writing the decrypted strings to the
existing text file and reading them in as you do now?

As I mentioned I need to achive this without changing the exisitng bat files. And the only way is to call a java program to decrypt password and at it to the env variables so bat file will get it.

Any suggestions?

 

Posts:8
Registered: 7/28/04
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:57 AM (reply 7 of 18)



 
Runtime.exec has a lot of overloadings. Two of them
allows you to specify the environment variables.


exec
public Process exec(String[] cmdarray,
String[] envp,
File dir)
throws IOExceptionExecutes the specified command and
arguments in a separate process with the specified
environment and working directory.
cmdarray - array containing the command to call and
its arguments.
envp - array of strings, each element of which has
environment variable settings in format name=value.
dir - the working directory of the subprocess, or null
if the subprocess should inherit the working directory
of the current process.

I had this sample program:

public class SetVarExample {

public static void main (String[] args) throws Exception {
String[] cmd_env= new String[] {"password="+"ABCD","Path=C:\\Sun\\AppServer\\jdk
bin"};

String cmd = "cmd /c SET ";
Runtime.getRuntime().exec(cmd,cmd_env);

System.out.println( "Finish ...." );
}
}

I tried it in a command prompt. But looks like when the program exits, it's a whole new process and so it does not retain the env variables set in the java program.
Any suggestions? Am I doing it worng?

thanks
 

Posts:319
Registered: 11/8/00
Re: setting windows environment variables from Java program  
Jul 30, 2004 7:59 AM (reply 8 of 18)



 
did you read the previous post?

see the API for Runtime.exec()

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[])
 

Posts:319
Registered: 11/8/00
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:00 AM (reply 9 of 18)



 
soz, should have refreshed
 

Posts:3,055
Registered: 18/06/98
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:09 AM (reply 10 of 18)



 
But looks like when the program exits, it's a whole new process and so it does not retain the env variables set in the java program.

In Windows and Unix you can't set environment variables from inside a program, and keep they set after the program exits. (Well, in Unix there is the "export" command in the shell, but it's changing a private copy of the environment variables inside th shell, that the shell uses to invoke other programs, and in Windows such environment modification can be done only by batch files.)
 

Posts:319
Registered: 11/8/00
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:18 AM (reply 11 of 18)



 
why don't you write a wrapper batch file

rem WrapperBatch set ENV1 = %2set ENV2 = %3 call YourBatchFile.bat


This should set the enviroment variables then start your bat file with these in memory.

You would then pass the variables as parameters to the batch file.
Runtime.exec("WrapperBatch.bat env1 env2");


Note: Not tested this and it has bee i while since i used this feature in a batch file

ps i think %1 is the name of the batch file
 

Posts:192
Registered: 30/05/01
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:23 AM (reply 12 of 18)



 
what about writing the decrypted strings to the
existing text file and reading them in as you do
now?

As I mentioned I need to achive this without changing
the exisitng bat files. And the only way is to call a
java program to decrypt password and at it to the env
variables so bat file will get it.

Any suggestions?

I might be missing something here (probably) but my understanding of your problem is...

1) You have a batch file that runs sqlplus using a userid and password that has been read from a text file.

2) The text file currently contains a non-encrypted userid and password.

3) Things are changing and the text file will now hold an encrypted userid and password.

So - why can't the java program just write the decrypted strings to the file that the batch file currently reads from?
 

Posts:8
Registered: 7/28/04
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:27 AM (reply 13 of 18)



 
I think I like the approach of the wrapper batch file. And for now, it seems to be the only way I could set the password as an env variable as noted in an earlier reply.

thank you all for the help.
 

Posts:8
Registered: 7/28/04
Re: setting windows environment variables from Java program  
Jul 30, 2004 8:30 AM (reply 14 of 18)



 

So - why can't the java program just write the
decrypted strings to the file that the batch file
currently reads from?

This will defeat the purpose of encrypting/decrypting the password. These batch jobs are run on another machine from the web interface that the user has access to. The approach I was thinking of was to set up password as an env variable, run the sql script and then set the password env variable to an empty string when done.
 
This topic has 18 replies on 2 pages.    1 | 2 | Next »