Running DOS program from java

Hi all,
I'm trying to execute a dos program using:
runtime.exec("dos_prog.exe")
THE PROBLEM : in this way I can run only
WINDOWS programs and NOT DOS progs.
What to do ???

Hi all!
I'm tring to run a file.bat that execute an exp from locale Oracle server. But when I run it, in dos consol it appaire just only the EXP USER/PASS ....., and don't execute never!! Really, the files of .dmp and of .log, they are created, ...but empty!!!! Why???
........ source .......
Runtime rt = Runtime.getRuntime();
Process rtProcess = rt.exec("cmd.exe /C file.bat");
// wait for command to terminate
try      {
     rtProcess.waitFor();
catch     (InterruptedException ire)
     System.err.println("Thread interrupted " + ire.getMessage());
// check its exit value
if (rtProcess.exitValue() != 0)
System.err.println("exit value was non-zero");
// close stream
bReader.close();
........ file.bat .......
@echo off
EXP USER/PSW FILE=EXPFILE.DMP LOG=FILELOG.LOG OWNER=MYOWNER
If you want, you car reply me directly at [email protected] - Thanks very much!!!

Similar Messages

  • Run a program from java code

    I want to run this program from my java code,
    In Linux(ubuntu) I write this command to open the program window
    $ paraFoam -case /home/saud/OpenFOAM/OpenFOAM-1.5/run/tutorials/icoFoam/cavity/In my java code I wrote it like this("/home/saud/OpenFOAM/OpenFOAM-1.5/bin/" is the path of the program):
    String command = "/home/saud/OpenFOAM/OpenFOAM-1.5/bin/paraFoam " +
              "-case /home/saud/OpenFOAM/OpenFOAM-1.5/run/tutorials/icoFoam/cavity/";
    final Process myProcess = Runtime.getRuntime().exec(command);
    BufferedReader buf = new BufferedReader(new InputStreamReader(
              myProcess.getInputStream()));
    String line;
    while ((line = buf.readLine()) != null) {
         System.out.print(String.format("%s\t\n", line));
    buf.close();
    int returnCode = myProcess.waitFor();
    System.out.println("Return code = " + returnCode); Return code = 0
    but the program is not opening
    Edited by: Saud.R on Apr 11, 2009 3:37 AM

    Welcome to the Sun forums.
    I am not sure of the answer to your question, but people who use Processes regularly, warn other users to ensure they are also doing something with the error stream of the process, otherwise it can hang as you describe.

  • Problem while running dos command from java program

    Dear friends,
    I need to terminate a running jar file from my java program which is running in the windows os.
    For that i have an dos command to find process id of java program and kill by using tskill command.
    Command to find process id is,
    wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
    This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
    when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
    Code to run this command is,
    public class KillProcess {
         public static void main(String args[]) {
              KillProcess kProcess = new KillProcess();
              kProcess.getRunningProcess();
              kProcess = new KillProcess();
              kProcess.readProcessFile();
         public void getRunningProcess() {
              String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
              try {
                   Runtime run = Runtime.getRuntime();
                   Process process = run.exec(cmd);
                   int i = process.waitFor();
                   String s = null;
                   if(i==0) {
                        BufferedReader stdInput = new BufferedReader(new
                               InputStreamReader(process.getInputStream()));
                        while ((s = stdInput.readLine()) != null) {
                         System.out.println("--> "+s);
                   } else {
                        BufferedReader stdError = new BufferedReader(new
                               InputStreamReader(process.getErrorStream()));
                        while ((s = stdError.readLine()) != null) {
                         System.out.println("====> "+ s);
                   System.out.println("Running process End....");
              } catch(Exception e) {
                   e.printStackTrace();
         public String readProcessFile() {
              System.out.println("Read Process File...");
              File file = null;
              FileInputStream fis = null;
              BufferedReader br = null;
              String pixieLoc = "";
              try {
                   file = new File("ProcessList.txt");
                   if (file.exists() && file.length() > 0) {
                        fis = new FileInputStream(file);
                        br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
                        String line;
                        while((line = br.readLine()) != null)  {
                             System.out.println(line);
                   } else {
                        System.out.println("No such file");
              } catch (Exception e) {
                   e.printStackTrace();
              return pixieLoc;
    }     when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
    if i give process.waitFor(), then it's in running mode and program is not completed.
    Colud anyone please tell me how to handle this situation?
    or Is there anyother way to kill the one running process in windows from java program?
    Thanks in advance,
    Sathish

    Hi masijade,
    The modified code is,
    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, "UTF-16");
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
                } catch (IOException ioe)
                    ioe.printStackTrace(); 
    public class GoodWindowsExec
        public static void main(String args[])
            try
                String osName = System.getProperty("os.name" );
                String[] cmd = new String[3];
                 if( osName.equals( "Windows 95" ) )
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                } else {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1]
                                   + " " + cmd[2]);
                Process proc = rt.exec(cmd);
                System.out.println("Executing.......");
                // 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();
    }when i execute the above code, i got output as,
    Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
    and keeps in running mode only.
    If i execute the same command in dos prompt,
    CommandLine
    ProcessId
    java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP  2468
    If i modify the command as,
    cmd.exe /C wmic process where "name='java.exe'" get commandline,processid  > 123.txt
    and keeps in running mode only.
    If i open the file when program in running mode, no contents in that file.
    If i terminte the program and if i open the file, then i find the processid in that file.
    Can you help me to solve this issue?

  • Oops - meant to say "Controlling Legacy DOS program from Java ..."

    Sorry - don't know what I wasn't thinking - muddled up my post title!
    As part of my final year project, I'm trying to control a camera mount automatically using stepper motors. This will then be used for robotic navigattion. Most of my code is written in Java, but I have been given some old C code to control the motors with. One loaded, it sits in an infinite loop that waits for keyboard input, resonds, loops, and so on ...
    I'm trying desperately to find a way of issuing commands to this program from my Java programs. I tried to implement a listening server in the C code but got a bit bogged down. Now wonder if I can use the
    exec and getOutputStream methods in Java to control the Dos program...?
    Any ideas very very welcome. I've reached hair pulling out time!
    Ben Barker

    Everything is now woeking...ish:
    I can now make the below java program bring up, for instance, windows calculator or notepad.
    I can't test it on the main code yet at I don't have the right computer in front of me. However, I decided to try to test it by runing cmd using ...exec("cmd"), then printing a command to the output stream to make windows calculator run. This caused precisely nothing to happen ...
    Any ideas?
    import java.io.*;
    class java_test
    public static void main(String[] args)
              try
              Process p= Runtime.getRuntime().exec("cmd");
              InputStream is= p.getInputStream();
              OutputStream os= p.getOutputStream();
              PrintStream printStr = new PrintStream(os);     
              printStr.println("calc.exe");
              catch (Throwable t)
              t.printStackTrace();
    }

  • Running a program from Java

    how do i launch one Java program from another?
    both .class files and the JRE directory are in the same directory

    You can use Runtime.exec() to run it as a seperate process, or you can always instantiate your secondary class. Do a search on the term "runtime.exec()" befure you go spawning another thread asking how to use it. That particular subject has been done to death many times already.

  • Problems running external programs from java

    Hello.
    I wrote a pair of perl scripts and a GUI in java to run them. The first perl script just read the files in one directory makes some changes to the names of the files and then group all this files in a set of new directories. The other perl scripts takes all this new files and calls BLAST sequence alignment program and perform some alignments among these sequences. I tested this scripts and they work fine.
    The problem comes when I try to run them for the JAVA GUI. I use RunTime and when I need to run the first perl script it all works well, but when The call to the second perl script is made the program fisishes without doing anything at all. I found out that the problem is that when running the script from the Java GUI it's not able to find BLAST program. So I guess that Java is not really starting a terminal session and it doesn't read my bash_profile to find out the path to my programs.
    So, my question is if anyone knows a method to tell Java to load all this paths in the bash_proflie file so all of my scripts work???.
    I have no idea is this can be done and how so any advice would be really wellcome.
    By the way, my java version is 1.4.2 and my OS is Mac OS X 10.3
    Thanks a lot , Julio

    Invoke /bin/sh -c and give it your program's full path with.
    (To understand what I've written, maybe reading
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html
    http://mindprod.com/jgloss/exec.html
    and
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
    (especially the exec(String[] cmdarray) method)
    might help)
    -T-

  • Running DOS command from JAVA

    Hi ,
    I would appreciate if anyone could tell me how to run DOS command such as "del" using JAVA language .Thank you.

    <steps onto soapbox>
    Surely for something like 'del' we should be advocating a non-OS specific method so we don't lose sight of Java's cross platform abilities.
    If it has to run an OS specific thing fine, but please look for a non OS specific solution first.
    <steps off soapbox>

  • Run dos command from java

    May I ask how I should write if I want to execute a dos command in java? The command is :
    "C:\Program Files\Real\realproducer\rmeditor -i abc.rm -d abc.txt". Should I use array?
    Thanks!

    why an array ?
    String command = "C:\\Program Files\\Real\\realproducer\\rmeditor -i abc.rm -d abc.txt";
    Process p = Runtime.getRuntime().exec( command );what about the seach function on the left side ? ;-)
    tobias

  • Run a program from java

    hi
    i have a program with extension .rcw and i want to run from a java program. can anyone show me the code that can help me to call this type of files or any type of files
    Thanks in advance.

    **  Here is a pure Windows solution
    **  Run the code and you will be taken to a tutorial
    that may provide
    **  a more generic solution
    public class WindowsProcess
         public static void main(String[] args)
              throws Exception
              String[] cmd = new String[4];
              cmd[0] = "cmd.exe";
              cmd[1] = "/C";
              cmd[2] = "start";
    cmd[3] =
    =
    "http://www.javaworld.com/javaworld/javatips/jw-javati
    p66.html";
    //          cmd[3] = "notepad";
    //          cmd[3] = "will.xls";
    //          cmd[2] = "a.html";
    //          cmd[3] = "file:/c:/java/temp/a.html";
    Process process = Runtime.getRuntime().exec( cmd
    md );
    }i really don't know what to say. You really help me.
    I'm really thankfull to you

  • Cannot run batch program from Java Application

    Hi All,
    Im trying to run a batch file under windows from my Java Program. The batch file is located in the following path:
    C:\Program Files\MyApp\runme.bat
    The above-entioned path contains white space which is not allowed in NT.
    Im trying following code:
    import java.io.*;
    public class Exec {
    public static void main(String args) {
    try {
    Runtime.getRuntime().exec("cmd /c start C:/Program Files/MyApp/runme.bat");
    }catch(Exception ex){ ex.printStackTrace();}
    How to run this file?
    Thanks in advance.
    Ketan Malekar

    or use the overload that takes an array of strings as argument
    String[] command = {"cmd", "/c", "start", "C:/Program Files/MyApp/runme.bat");
    Runtime.getRuntime().exec(command);

  • Running COBOL programs from java

    I am on a project at the moment where we want the client-server to be written in Java on Solaris Sparc UNIX machines. The thing is we want the server to start a COBOL program and the output from the COBOL program will be the reply to the client. Is this possible and if so how?
    Any help gratefully received
    Chris
    PS We are not looking to buy expensive tools

    JNI would be necesssary if you want to call cobol routines from your java program or vice versa. If you want to communicate with another process then JNI is not really necessary. Indeed it is a very difficult API to master and unnecessary complexity. Well the way I was thinking was that your cobol program can accept input from standard input and write to standard output (if there is such a thing in cobol). You can then read the inputstream of the process which you spawned using runtime.getRuntime.exec("mycobolprogram) mechanism...This should work fairly realiably. I am not sure if this is what you are looking for but give it a try by writing a small cobol program which outputs "Hello World" to standard output and then try reading it from a test java program...

  • Running another program from Java on OSX

    I have an application that allows the user to specify an external program to run using the Process class, they just have to xpecify the path.
    On OSX most of the apps are in .app packages with the contents hidden to the filechooser. What command do I need to run to start (for example ITunes) an application

    Thanks, this is very wierd but compard to other OSes seems to work

  • Dos command from java

    hi all
    how do i run dos commands from java??????????

    Using Runtime#exec().
    Also see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html

  • How to run native program with Java program?

    Hello
    I've got following problem. I'd like to write file browser which would work for Linux and
    Windows. Most of this program would be independent of the system but running programs not. How to run Linux program from Java program (or applet) and how to do it in Windows?.
    Cheers

    Try this:
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("ls -l");
    InputStream stream = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stream);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ( (line = br.readLine()) != null) .....
    "if the program you launch produces output or expects input, ensure that you process the input and output streams" (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)

  • Call visual prolog program from java

    Hi friends,
    is there someone help me to call ( run) prolog program from java.
    i write a parser with Javacc parser generator and stored parsed information in prolog database and I want create some query in java that work in the prolog file.
    how can i combine java and prolog programs. i used visual prolog.
    Can someone help me?
    Thanks you in advance.
    yours sincerely,
    ksu

    Since visual prolog can produce dll's, you can use JNI:
    http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html

Maybe you are looking for