The Artima Developer Community
Sponsored Link

Java Answers Forum
Using envirnoment variables in Runtime.exec(cmdLine)

1 reply on 1 page. Most recent reply: Aug 2, 2002 7:54 AM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Rohit

Posts: 38
Nickname: rohit
Registered: May, 2002

Using envirnoment variables in Runtime.exec(cmdLine) Posted: Aug 1, 2002 10:48 PM
Reply to this message Reply
Advertisement
Hi Guys,
I m trying to create a process using Runtime

Runtime rt = Runtime.getRuntime();
Process p1 = rt.exec(cmdLine);

This cmdLine uses some env variable,which are declared in the same JVM from which this whole prog is kicked of,but it seems its not picking them up,

e.g cmdLine is something like
String cmdLine = " java + " -cp "+ "\"%DASLIB%\\dataaccessservices.jar;%JINIJARS%;%JDOMJARS%\" -Djava.rmi.server.codebase=\"%DASWEB%/DASConnectService-dl.jar %DASWEB%/db2java.zip %DASWEB%/workshop-dl.jar %DASWEB%/util-dl.jar %DASWEB%/exception-dl.jar\" myclasstorun";

is it possible to use the env vari,and whats the prob with my code then?


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Using envirnoment variables in Runtime.exec(cmdLine) Posted: Aug 2, 2002 7:54 AM
Reply to this message Reply
Remember answering a question like this. Anyways, there are a few things to be noted. Runtime.exec() is not quite the same as system() in C as you'll see. And, to use environment variables, I dont think its a good idea to put them in the code because they can change at any moment. Instead, use System.getProperty("java.class.path") to get the $CLASSPATH at that moment. Heres a class that can compile and run another class (works well on Win2000 and JDK1.3.1_01):
import java.io.*;
 
public class DoClass
{
	private void execute(String[] cmd)
	{
		String output = null;
		try
		{
			String line;
			Runtime rt = Runtime.getRuntime();
			Process process = rt.exec(cmd);
			InputStreamReader isr = new InputStreamReader( process.getInputStream() );
			OutputStreamWriter osw = new OutputStreamWriter( process.getOutputStream() );
 
			BufferedReader br = new BufferedReader( isr );
			BufferedWriter bw = new BufferedWriter( osw );
 
			while( ( line = br.readLine() ) != null )
			{
				System.out.println( line );
			}
 
			while( ( line = br.readLine() ) != null )
			{
				System.out.println( line );
			}
 
			int exitValue = process.waitFor();
			System.out.println( "ExitValue: " + exitValue );
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}
	}
 
	public void compileClass()
	{
		String cmd[] = new String[4];
		cmd[0] = "javac";
		cmd[1] = "-classpath";
		cmd[2] = System.getProperty("java.class.path");
		cmd[3] = "HelloWorld.java";
		System.out.println("Compiling Class...");
		execute(cmd);
	}
 
	public void runClass()
	{
		String cmd[] = new String[4];
		cmd[0] = "java";
		cmd[1] = "-classpath";
		cmd[2] = System.getProperty("java.class.path");
		cmd[3] = "HelloWorld";
		System.out.println("Running Class...");
		execute(cmd);
	}
 
	public static void main(String s[])
	{
		DoClass doer = new DoClass();
		doer.compileClass();
		doer.runClass();
	}
}


My HelloWorld.java looks like this:
class HelloWorld
{
   public static void main(String args[])
   {
	   System.out.println("Ran HelloWorld!");
   }
}


P.S: Charles Bell posted a class that runs Runtime.exec() on several platforms I believe. You might want to check that too.

Flat View: This topic has 1 reply on 1 page
Topic: exception handling Previous Topic   Next Topic Topic: jdbc:odbc:Sales

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use