Using Runtime class

Hi,
i would like to run a an external java program from within another java program,
here is the structure of my called parogram
c:/temp/jetty
/lib/*.jar
start.jar
my caller program have to invoke the start.jar, but this jar uses the lib folder
so my code construct a java command with the -cp options giving all the jar, and finally use th runtime class to call start.jar
here is the code :
String command = "java -cp D:/temp/jetty/lib/jetty-6.1.0.jar;D:/temp/jetty/lib/etc/another.jar -jar D:/temp/jetty/lib/jetty-6.1.0/start.jar"
Runtime runtime = Runtime.getRuntime();     
runtime.exec(command);
it doesnt work, (classnot found error!!!) evenif i include in the cp option all the jar,
my question is how to force the runtime to start the execution at c:/temp/jetty ??
can anyone help

Try this for ideas
package testing;
import java.io.IOException;
public class Test
  public static void main(String[] args) throws IOException
    Runtime.getRuntime().exec("java -jar AnotherJar.jar");
}and
package testing;
import javax.swing.JOptionPane;
public class Another
  public static void main(String[] args)
     JOptionPane.showMessageDialog(null, "Another .jar running", "Blaa blaa", JOptionPane.INFORMATION_MESSAGE);
}Then compile both, create manifests for both, create the jars (name the .jar corresponding to class Another as AnotherJar.jar), put the jars in the same directory and run the .jar that corrsponds to class Test.
edit
And obviously, in your case, you need to add the line
Class-Path: lib/*.jar
to the manifest of the program that needs the jar in directory lib.
Message was edited by:
duckbill

Similar Messages

  • Executing a command using Runtime Class

    How to execute a command on a differnet machine with different ipaddress using Runtime Class
    My code is
    String[] cmd = new String[3];
    cmd[0] = "192.1...../c:/WINNT/system32/cmd.exe" ;
    cmd[1] = "/C" ;
    cmd[2] = args[0];
    Runtime rt = Runtime.getRuntime();
    System.out.println("Execing " + cmd[0] + " " + cmd[1]
    + " " + cmd[2]);
    Process proc = rt.exec(cmd);
    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    This is not Working

    I have same issue. Actually when I use cmd.exe /c set in java code and if I run the java code in DOS propmt, it retrieves all latest user Environment variable values. But if I run the code in windows batch file, it is not retrieveing the latest user environment values until I reboot my computer, Do you know how to get user environment value with out rebooting machine??????

  • FTP using Runtime class ...Please Help ??

    Hi,
    I am trying to ftp a file programatically.
    I am trying to use Runtime class but facing problems
    in it.This is what I am trying to do :
    Runtime rr = Runtime.getRuntime();
    String[] cmds = new String[2];
    cmds[0]="username=rahmed";
    cmds[1]="password=prpas";
    try{
    Process p = rr.exec("ftp 192.168.1.18",cmds);     
    rr.exec("put vv.txt");
    This does not work ??
    Is there any way to make it work ? Or is there any
    other way to ftp a file programatically ??
    Thanks in adavance..
    Regards
    Rais

    Under Linux/Unix at least, it is good to use the switches -n and -i with the ftp client acually meant for intercative usage.
    -i Turns off interactive prompting during multiple file transfers.
    -n Restrains ftp from attempting ``auto-login'' upon initial connection.
    I do "user <myuser> <mypassword>" then from the script.
    I am happily using ftp this way from shell-scripts in my projects.
    scp is however better than ftp: it does not send plain text passwords over the net, it support key-based login, its encrypts the data.

  • Execute an external program using Runtime class

    How to execute an external java program using Runtime class?
    I have used ,
    Process p=Runtime.getRuntime().exec("c:/j2sdk1.4.0/bin/helloworld.java ");
    But it throws a runtime IOException error:2 or error:123.
    Help me with the code. Thanks in advance.

    Create Runtime Object and attach to system process.Try this code
    import java.io.*;
    public class ExecuteExternalApp {
      public static void main(String args[]) {
                try {
                    Runtime rt = Runtime.getRuntime();
                    //Process pr = rt.exec("cmd /c dir");
                    Process pr = rt.exec("c:\\ yamessenger.exe"); //give a valid exe file
                    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                    String val=null;
                    while((val=input.readLine()) != null) {
                        System.out.println(val);
                    int exit = pr.waitFor();
                    System.out.println("Exited with error code "+exit);
                } catch(Exception e) {
                    System.out.println(e.toString());
                    e.printStackTrace();
    }Edited by: anishtomas on Feb 3, 2009 9:34 PM
    Edited by: anishtomas on Feb 3, 2009 9:37 PM

  • BCP data into a file using Runtime class

    Hi,
         I have a problem in using the Runtime class.
    I am trying to bcp a table's data into a file.I am working on a Unix environment.
    My bcp is not getting completed fully.The total records to be bcped is 1 million,but only one lakh records is getting bcped and then it hangs up..But if i issue the bcp command from my telnet session it is bcping it to the file without any problem.Can anyone help me out on how to overcome this..
    Is there anything specific with Runtime class..I am pasting the code that i tried out below.
    String l_s_bcpQuery="bcp mubstage.dbo.HousingUnitSampleMarket out /dun/d3nmb0/mariaps/subracheckprototype -c -t~ -Umariaps -Prykwz5ba -SD3NMB_MUB";
    try{
    Runtime time=Runtime.getRuntime();
    Process p=time.exec(l_s_bcpQuery);
    p.waitFor();
    System.out.println("The exit value is:"+p.exitValue());
    }catch(Exception ioe){
         ioe.printStackTrace();
         System.out.println("IOException"+ioe.getMessage());

    you might need to capture the stout & sterr from the process. see http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Process.html, or many other similar questions on this forum.
    try this:
    try{
    Runtime time=Runtime.getRuntime();
    Process p=time.exec(l_s_bcpQuery);
    BufferedReader stout = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader sterr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String out = stout.readLine();
    String err = sterr.readLine();
    while((out != null)||(err != null))
    if(out != null)
    System.out.println(out);
    if(err != null)
    System.err.println(err);
    out = stout.readLine();
    err = sterr.readLine();
    int exit_value = p.waitFor();
    System.out.println("The exit value is:"+ exit_value);
    }catch(Exception ioe){
    ioe.printStackTrace();
    System.out.println("IOException"+ioe.getMessage());
    }

  • Unable to start Tomcat using runtime class in windows 98

    hi,
    I am using a runtime class which calls a batch file, that calls statup.bat for starting tomcat.
    This works perfectly in windows NT,2k. But on win 98, the tomcat window opens and closes within no sometime.
    In my class i call the first batch file starter.bat using
    Runtime.getRuntime().exec("%COMSPEC% /c start e:/starter.bat");
    (I tried with command.com also...)
    Starter.bat has the line
    call E:\jakarta-tomcat4.1\bin\startup.bat.
    Is there anyother way to start the tomcat process and keep it running on windows 98. Anyone with anyclues pls help.
    Thanks in advance.

    This is the Runtime class i am using... Sometimes tomcat gets closed and sometimes just hangs... (putting it in for better understanding..)
    public class Starter {
         public static void main(String[] args) {
              String com = "%COMSPEC% /c start
    C:\\PACS\\SFAApp\\tomcat\\startup.bat";
                   try     {               
                        Runtime rt = Runtime.getRuntime();
                        Process pr = rt.exec (com);//Starting tomcat
                        try
                             pr.waitFor();     
                        catch (Exception e)
                             e.printStackTrace();
                   catch (IOException ie) {
                        System.out.println("IOException caught" + ie);
                        ie.printStackTrace();

  • Using Runtime class for output

    Hello,
    I am using Runtime.getRuntime().exec("msxml3.dll,load myfile.xml");
    where "load" is the method. I get an error message as "IOException CreateMethod" error=2.
    Can some one help me out?
    Thanks
    Mathew

    Hallo Mathew,
    The part of the error message CreateMethod error=2 almost certainly comes from Windows. I suspect that you could be feeding the wrong parameters to msxml3.dll. Have you tried the "msxml3.dll,load myfile.xml" from a command line prompt? Another possibility is that, when you call exec(), you do not get a command prompt. Instead the program starts the exe (or in this case a dll) as a separate process. Sometimes it helps to embed such commands as follows:
    Runtime.getRuntime().exec ("cmd /K msxml3.dll,load myfile.xml");(when it all works, replace the /K with a /C, to get rid of the window)

  • Error in creating a process using runtime class - please help

    Hi,
    I am experimenting with the following piece of code. I tried to run it in one windows machine and it works fine. But i tried to run it in a different windows machine i get error in creating a process. The error is attached below the code. I don't understand why i couldn't create a process with the 'exec' command in the second machine. Can anyone please help?
    CODE:
    import java.io.*;
    class test{
    public static void main(String[] args){
    try{
    Runtime r = Runtime.getRuntime();
         Process p = null;
         p= r.exec("dir");
         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
         System.out.println(br.readLine());
    catch(Exception e){e.printStackTrace();}
    ERROR (when run in the dos prompt):
    java.io.IOException: CreateProcess: dir error=2
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Win32Process.java:63)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:550)
    at java.lang.Runtime.exec(Runtime.java:416)
    at java.lang.Runtime.exec(Runtime.java:358)
    at java.lang.Runtime.exec(Runtime.java:322)
    at test.main(test.java:16)
    thanks,
    Divya

    As much as I understand from the readings in the forums, Runtime.exec can only run commands that are in files, not native commands.
    Hmm how do I explain that again?
    Here:
    Assuming a command is an executable program
    Under the Windows operating system, many new programmers stumble upon Runtime.exec() when trying to use it for nonexecutable commands like dir and copy. Subsequently, they run into Runtime.exec()'s third pitfall. Listing 4.4 demonstrates exactly that:
    Listing 4.4 BadExecWinDir.java
    import java.util.*;
    import java.io.*;
    public class BadExecWinDir
    public static void main(String args[])
    try
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("dir");
    InputStream stdin = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stdin);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    System.out.println("<OUTPUT>");
    while ( (line = br.readLine()) != null)
    System.out.println(line);
    System.out.println("</OUTPUT>");
    int exitVal = proc.waitFor();
    System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
    t.printStackTrace();
    A run of BadExecWinDir produces:
    E:\classes\com\javaworld\jpitfalls\article2>java BadExecWinDir
    java.io.IOException: CreateProcess: dir error=2
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Unknown Source)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at BadExecWinDir.main(BadExecWinDir.java:12)
    As stated earlier, the error value of 2 means "file not found," which, in this case, means that the executable named dir.exe could not be found. That's because the directory command is part of the Windows command interpreter and not a separate executable. To run the Windows command interpreter, execute either command.com or cmd.exe, depending on the Windows operating system you use. Listing 4.5 runs a copy of the Windows command interpreter and then executes the user-supplied command (e.g., dir).
    Taken from:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Problem while trying to execute Java class in JSP using  RunTime Class

    Hi,
    I want to execute a JAVA class through a JSP. For this I am using following code ....
    JSP (AAA.jsp) CODE ............
    try
    String[] cmd = new String[3];
    cmd[0] = "cmd.exe" ;
    cmd[1] = "/C" ;
    cmd[2] = "java -DPeBS_CONFIG_HOME=D:/CASLIntegration/PeBS/srcvob/PeBS/config Service_Statement_Application 22/May/2001 22/May/2003";
    Runtime rt = Runtime.getRuntime();
    System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
    Process proc = rt.exec(cmd);
    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    catch (Throwable t)
    t.printStackTrace();
    StreamGobbler THread class Code ..........
    import java.util.*;
    import java.io.*;
    public class StreamGobbler extends Thread
    InputStream is;
    String type;
    StreamGobbler(InputStream is, String type)
    this.is = is;
    this.type = type;
    public void run()
    try
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe)
    ioe.printStackTrace();
    I have successfully compiled and placed the class file for the above class in JSP's servlet engine. However, when I execute the JSP through explorer Web Browser, I get following compile time error:
    An error occurred between lines: 36 and 86 in the jsp file: /casl/LocalApp/VehicleServiceStmt/AAA.jsp
    Generated servlet error:
    D:\Tomcat\work\localhost\_\casl\LocalApp\VehicleServiceStmt\AAA$jsp.java:118: No constructor matching StreamGobbler(java.io.InputStream, java.lang.String) found in class StreamGobbler.
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
    ^
    An error occurred between lines: 36 and 86 in the jsp file: /casl/LocalApp/VehicleServiceStmt/AAA.jsp
    Generated servlet error:
    D:\Tomcat\work\localhost\_\casl\LocalApp\VehicleServiceStmt\AAA$jsp.java:121: No constructor matching StreamGobbler(java.io.InputStream, java.lang.String) found in class StreamGobbler.
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    I am unable to determine the reason of why the constructor which exists in the StreamGobbler Class is not recevied in JSP. If I try to write the same code in JSP as a JAVA class, keeping StreamGobler class same, the programme executes successfully.
    Please help me find solution to this at the earliest. Thanks in advance,
    Prachi

    Thanks,
    I got it working by making the constructor Public.
    -Prachi

  • Need help in using Runtime class

    Hi all,
    I am facing problem with JRE versions of websphere and SUN JRE. Let me explain my problem.
    We have an application working fine in websphere, Now my organization wants to migrate this app to websphere. This application downloads some set of jar files to the client side when the user hits the server with some url. These jar files starts th swing based application in the client side , at the same it will try to communicate with EJB's deployed in the server(Now websphere) to get the data for menus and trees in the swing application. Because of different JRE in the websphere commucation problems like class cast exception are happening. Now we are downloading the the Websphere JRE to the client side and trying to start the application with the downloaded JRE(Even it is not required in case of weblogic in the exiting application also they are downloading the sun JRE). Here the problem starts for me. The execute method of Process class is not able to detect the main class. The waitFor method is returning non zero value. Here is my method.
    Can anybody guess what's wrong going on with this code.
    public Process spawnProcess(String szSpawnCommand) throws
    Exception
              szSpawnCommand = "C:\\ccc\\JRE\\bin\\javaw.exe com.att.suite.client.SuiteAppManager";
              System.out.println("starting processs "+szSpawnCommand);
    Process oProcess = null;
    try
    // oProcess = Runtime.getRuntime().exec(szSpawnCommand);
         Runtime rt = Runtime.getRuntime();
         System.out.println("Got runtime.....");
         oProcess = rt.exec("c:/ccc/JRE/bin/java com.att.suite.client.SuiteAppManager");
         //oProcess.waitFor();
    //oProcess = rt.exec("c:/ccc/JRE/bin/javaw -version");
    catch (Exception ex)
    // Throw an exception with the reason why the process
    // couldn't be spawned
    // The original message here confused end users - almost always
    // occurs due to lack of memory and we cannot determine actual
    // reason OS failed to spawn a process.
    // Changed to correct LCR MR#: 437
    // throw new Exception("Unable to spawn process with command: \"" +
    // szSpawnCommand + "\".\nSystem Reason: " + ex.getMessage());
    /*throw new Exception("You have insufficient memory to " +
    "launch this program. \nPlease quit some other " +
    "applications and try again.");*/
         System.out.println("In oProcess.... ");
    ex.printStackTrace();
    // Wait for a second to give the new process a chance
    // to run properly
    try
    Thread.sleep(1000);
    catch (Exception ex)
    final DataInputStream oProcStdOutStream = new DataInputStream(
    oProcess.getInputStream());
    final DataInputStream oProcStdErrStream = new DataInputStream(
    oProcess.getErrorStream());
    Thread oProcStdOutStreamThread = new Thread()
    public void run()
    try
    String szLine = null;
    while ((szLine = oProcStdOutStream.readLine()) != null)
    System.out.println("[FromProc1] " + szLine);
    catch (Exception ex)
         System.out.println("In oProcStdOutStream");
    ex.printStackTrace();
    oProcStdOutStreamThread.start();
    Thread oProcStdErrStreamThread = new Thread()
    public void run()
    try
    String szLine;
    while ((szLine = oProcStdErrStream.readLine()) != null)
    System.out.println("[FromProc2] " + szLine);
    catch (Exception ex)
    ex.printStackTrace();
    oProcStdErrStreamThread.start();
    // See if the process is still running properly
    int nProcessExitValue = 0;
    try
    nProcessExitValue = oProcess.exitValue();
         // nProcessExitValue = oProcess.waitFor();
         System.out.println("nProcessExitValue : "+nProcessExitValue);
    catch (Exception ex)
    // This means that the process is still running which
    // we'll consider a good sign.
    nProcessExitValue = 0;
    System.out.println("In nProcessExitValue");
    ex.printStackTrace();
    // Check the process exit value
    if (nProcessExitValue != 0)
    // Throw an exception with the return value of the spawned
    // process.
    // The original message here confused end users - almost always
    // occurs due to lack of memory and we cannot determine actual
    // reason OS failed to spawn a process.
    // Changed to correct LCR MR#: 437
    // throw new Exception("Unable to spawn process with command: \"" +
    // szSpawnCommand + "\".\nProcess Exit Value: " +
    // nProcessExitValue);
    throw new Exception("You have insufficient memory to " +
    "launch this program. \nPlease quit some other " +
    "applications and try again.");
         //System.out.println("In nProcessExitValue!=0");
    Here is the error :
    starting processs C:\ccc\JRE\bin\javaw.exe com.att.suite.client.SuiteAppManager
    Class loaded...
    Got runtime.....
    nProcessExitValue : 1
    [FromProc2] The java class is not found: com/att/launch/jre/Sample
    java.lang.Exception: You have insufficient memory to launch this program.
    Please quit some other applications and try again.
         at com.att.launch.jre.LaunchApp.spawnProcess(LaunchApp.java:525)
         at com.att.launch.jre.LaunchApp.spawnCJAS(LaunchApp.java:366)
         at com.att.launch.jre.LaunchApp.main(LaunchApp.java:171)
    Please help me, I am struggling with this problem from the long time. Or can any body suggest different way to solve this problem instead of downloading the JRE to the client side.
    Bhaskar

    Hi ,
    I am facing problem with JRE versions of websphere and SUN JRE. Let me explain my problem.
    We have an application working fine in websphere, Now my organization wants to migrate this app to websphere. This application downloads some set of jar files to the client side when the user hits the server with some url. These jar files starts th swing based application in the client side , at the same it will try to communicate with EJB's deployed in the server(Now websphere) to get the data for menus and trees in the swing application. Because of different JRE in the websphere commucation problems like class cast exception are happening.
         Now we are downloading the the Websphere JRE to the client side and trying to start the application with the downloaded JRE(Even it is not required in case of weblogic in the exiting application also they are downloading the sun JRE).
    Here the problem starts for me. The execute method of Process class is not able to detect the main class. The waitFor method is returning non zero value. Here is my method.
    Can anybody guess what's wrong going on with this code
    public Process spawnProcess(String szSpawnCommand) throws
    Exception
              System.out.println("starting processs "+szSpawnCommand);
    Process oProcess = null;
    try
         Runtime rt = Runtime.getRuntime();
         System.out.println("Got runtime.....");
         oProcess = rt.exec("c:/ccc/JRE/bin/java com.att.suite.client.SuiteAppManager");
    catch (Exception ex)
    throw new Exception("You have insufficient memory to " +
    "launch this program. \nPlease quit some other " +
    "applications and try again.");      
    // Wait for a second to give the new process a chance
    // to run properly
    try
    Thread.sleep(1000);
    catch (Exception ex)
    final DataInputStream oProcStdOutStream = new DataInputStream(oProcess.getInputStream());
    final DataInputStream oProcStdErrStream = new DataInputStream(oProcess.getErrorStream());
    Thread oProcStdOutStreamThread = new Thread()
    public void run()
    try
    String szLine = null;
    while ((szLine = oProcStdOutStream.readLine()) != null)
    System.out.println("[FromProc1] " + szLine);
    catch (Exception ex)
         System.out.println("In oProcStdOutStream");
    ex.printStackTrace();
    oProcStdOutStreamThread.start();
    Thread oProcStdErrStreamThread = new Thread()
    public void run()
    try
    String szLine;
    while ((szLine = oProcStdErrStream.readLine()) != null)
    System.out.println("[FromProc2] " + szLine);
    catch (Exception ex)
    ex.printStackTrace();
    oProcStdErrStreamThread.start();
    // See if the process is still running properly
    int nProcessExitValue = 0;
    try
    nProcessExitValue = oProcess.exitValue();
         // nProcessExitValue = oProcess.waitFor();
         System.out.println("nProcessExitValue : "+nProcessExitValue);
    catch (Exception ex)
    // This means that the process is still running which
    // we'll consider a good sign.
    nProcessExitValue = 0;
    System.out.println("In nProcessExitValue");
    ex.printStackTrace();
    // Check the process exit value
    if (nProcessExitValue != 0)
    throw new Exception("You have insufficient memory to " +
    "launch this program. \nPlease quit some other " +
    "applications and try again.");
    Here is the error :
    starting processs C:\ccc\JRE\bin\javaw.exe com.att.suite.client.SuiteAppManager
    Class loaded...
    Got runtime.....
    nProcessExitValue : 1
    [FromProc2] The java class is not found: com/att/launch/jre/Sample
    java.lang.Exception: You have insufficient memory to launch this program.
    Please quit some other applications and try again.
         at com.att.launch.jre.LaunchApp.spawnProcess(LaunchApp.java:525)
         at com.att.launch.jre.LaunchApp.spawnCJAS(LaunchApp.java:366)
         at com.att.launch.jre.LaunchApp.main(LaunchApp.java:171)
    Please help me, I am struggling with this problem from the long time. Or can any body suggest different way to solve this
    problem instead of downloading the JRE to the client side.
    Bhaskar

  • Run external programs using runtime class

    Okay, I'm experiencing a really annoying problem with java.lang.runtime
    I'm building a GUI that needs to run some external programs, via a button say. These generally produce a text file or something, so I don't need to stream the output or anything (at least I'm assuming I don't?). Should be very simple...
    So at the terminal (bash) I would type ./programName , and everything will run hunkey dorey.
    In my code then, natrurally, I write
    String cmd = "./programName";
    Process p = runtime.getRuntime().exec(cmd);
    But low and behold...nothing happens. What is going on here, and how do I get around it! ??
    (On windows incidentally, it's no problem at all and works absolutely fine. But when I go over to mac, which is what I need to use, I'm screwed - only adding to the annoyance!)
    Any help would be much appreciated as I have a deadline looming!!

    You need to read the 4 sections of [http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html] and implement the recommendations. Failure to implement all the recommendations will cause you grief.
    P.S. The fragment of code you have posted shows that you have fallen for at least 4 of the traps.

  • How can i launch another java application using Runtime class?

    I created a java process which invokes another java application called Launch as shown below,
    Process p=Runtime.getRuntime().exec("java -classpath=C:\\Program Files\\bin\\nettools\\ui\\updates Launch");
    But it is not working...can any one help me on this?
    Edited by: deepakchandaran on Sep 20, 2007 7:10 AM

    You could search the forum and Google.
    it has been answered before.
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Java programming - Runtime class

    hi
    I am not able to copy a file using Runtime class in java.lang
    i have used
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("copy c:/venk1/pmrpay.jar F:/");
    pr.waitFor();
    when running the program i am getting this error message:
    create process: copy c:/venk1/pmrpay.jar F:/ error = 2
    kindly help me
    venkat

    There is no copy.exe on your system. Copy is command interpreted with command.com. So you need to try something like
    Process pr = rt.exec("cmd /c copy c:/venk1/pmrpay.jar F:/");
    See
    cmd /?
    for details... Hope this will help you!

  • How to use Runtime execute DOS command?

    Hello,
    I can not use Runtime class execuste Dos command such as copy and dir.
    Is there any suggestion?
    Thanks in advance.

    execute the command with cmd /c (winNT, win2k) or command /c (win9x). Example: "cmd /c dir"
    details: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Runtime class behaving erratic

    hi all,
    I am writing a simple network shell in java...it runs well for any dos commands but it doesn't executes a .exe file (using runtime class) while the program is active but when i quit the program the .exe file called before runs.
    what's the problem...

                            else if(array.length == 2 && array[1] == ':'&& array[2] == '\\')
                                   for (int index = 0; index < roots.length; index++)
                                        if(string1.equals(roots[index].toString()))
                                            System.setProperty("user.dir", string1);
                                            pwd = System.getProperty("user.dir");
                                            output.println(pwd);
                                            output.flush();
                            else if(array[0] == 'c' && array[1] == 'd' && array.length > 2)
                                       System.setProperty("user.dir", string1.substring(3));
                                //System.out.println(string1.substring(3));
                                pwd = System.getProperty("user.dir");
                                output.println(pwd);
                                //output.flush();
                            else if(string1.endsWith(".exe") || string1.endsWith(".avi"))
                                System.out.println(pwd+"\\"+string1);
                                       String run1 = pwd+"\\"+string1;
                                       /* Thread t = new Thread(new Run(run1));
                                       t.start(); */
                                       rtime.exec(run1);////******* HERE LIES THE PROBLEM @run1 path of the .exe file
                                       continue;
                                  else
                                       commands[2] = string1;
                                p = rtime.exec(commands);                           
                                Scanner scan = new Scanner(p.getInputStream());
                                       String out = "";
                                while(scan.hasNextLine())
                                            out = out + scan.nextLine()+"~";
                                            //output.flush();
                                output.println(out);
                                       output.flush();
                                  output.println("end");
                                  output.flush();
            catch (IOException ex)
                ex.printStackTrace();
         public void run()
              try
                   Runtime.getRuntime().exec(string);
              catch(IOException e)
        private static DatagramSocket clientSocket;
         private static MulticastSocket multiSocket;
        private static int boundPort = 8000;
        private static int multiPort = 5678;
         private static InetAddress group;
         private static String string = "active";
        private static String pwd;
         private static PrintWriter output;
        private static Scanner input;
        private static Process p;
        private static DatagramPacket packet;
    }

Maybe you are looking for