Waiting a Dos Command?

Hi;
In one of our projects, we have to call a dos command in java. That seems ok however there is a problem about code. It gives an exception because the remaning program goes without waiting the dos command's completion.
Runtime r = Runtime.getRuntime();
r.exec("cmd /C sort 1.txt /O 2.txt");Above code, makes a sort in 1.txt and saves it to 2.txt.. My code goes without waiting the 2.txt to be created and gives an exception like couldn't found 2.txt . Thanks for any help, i hope i could explained clearly what i wanted to ask..

Yea you're right thanks for answer. But yesterday Petter a.k.a wildfrog from Codeguru forums posted this solutions. Anyway i'm putting it here if someone needs it.
To wait until the 'child process' has completed, you can do something like this:
Runtime r = Runtime.getRuntime();
Process p = r.exec("cmd /C sort 1.txt /O 2.txt");
// wait for it to complete
try
    p.waitFor();
} catch (InterruptedException e)
    // wait was interrupted
}- petter

Similar Messages

  • Thread not waiting for DOS process

    Hi
    In program I am using Threads. Here all threads will start and execute one DOS command. My problem is that all those thread needs to wait for DOS command to finish its process and then only thread should return.
    I have created ThreadGroup and added all threds. And I am checking it by using
    ThreadGroupVar.activeCount()!=0 so that I can come to know all thread processing is over or not.
    Threads are coming back to main process as soon as they start DOS command ..they are just not waiting it to be finish DOS job.
    Please guide me on this
    Regards,
    Niraj

    Create a collection of the Threads then iterate through it using join() on each one. When you have iterated though all the Threads they will all have completed.

  • How can i make the results of dos command print to text area in a gui????

    I need to get the results from a command line application to print in a text area located in a gui... i have the following code written, which should run the command... if run from a dos window, the command prints the list of what's in a jar file to the screen.... i want that info that is printed to go in a text area which i have named textBox which is global to the program.. i know textBox.appends() is what i should use but how do i retrieve the info from the dos window to put in the appends method?
    The code is below for the method..
    Thanks,
    DH
         /*     Runs the DOS Command for listing a JAR File     */
         public void jarList ( ) // Needs To Be Fixed!!
              listFieldText = listField.getText();
              String command;      
              command = "jar tf " + listFieldText;
              System.out.println(command);
              Runtime rt = Runtime.getRuntime();
              try
                   Process child = rt.exec(command);
                   child.waitFor();
                   System.out.println("Process exit code is: " + child.exitValue());
              catch(IOException e)
                   System.err.println("IOException starting process!");
              catch(InterruptedException e)
                   System.err.println("Interrupted waiting for process!");
    }

    Replace child.waitFor()with:          BufferedReader br=new BufferedReader(new InputStreamReader(child.getInputStream()));
              String s="";
                    while((s=br.readLine())!=null) {
                        textbox.append(s);
                    }Mark

  • DOS command from java applet

    Hi, I need to perform a DOS command from Java (using WFC if needed)...
    I have no idea, so please help!
    Thank you.

    OH OH OH WAIT! It is an Applet! i think there will be some security issues here! Sure enough you can't execute commands on the system from an applet unless you make some changes on the java.policy files on the client machine (that means, if your applet is in a web page, every machine which view your page).

  • DOS COMMAND IN ANOTHER MACHINE

    Hello!
    how can I execute a DOS command in another machine which is in my same network?? I need it because I have to execute an oracle report from my java program but the Oracle Reports software is in another machine ..... ( really i don't know if it's possible to do this ....)
    thanks!!

    As I see it you must have an application running on that other machine that will wait for remote commands.
    You can implement a simple application that opens/listens on a socket and executes commands that are coming in - this will run on the remote machine.
    Then you will have to connect to that socket from your machine and send commands to it.
    Maybe there are other ways but that's the first that came to my mind
    You can [url http://onesearch.sun.com/search/developers/index.jsp?col=devforums&qp_name=Java+Programming&qp=forum%3A31&qt=%2Bexecute+%2Bcommand+%2Bremote] do a search  around here to find more solutions
    And please DO NOT USE CAPITAL LETTERS when posting... It is considered as shouting.
    Mike

  • Help! invoke dos command!!

    i want to implement a notepad in java language, it can invoke a dos command(ping.exe) to some IP addresses stored in a text file, and store the ping results into a text file, how can i invoke a dos command and store the execute results into a text file???

    If you want to execute an external ping command and collect the result, it can be done by using the Runtime class. Runtime's exec method returns a Process supplying you with an input stream containing the ping result. You must make sure to read the output on the go (to avoid blocking the entire process).
    Here's an outline:
    import java.io.*;
    public class Ping {
        public static void main(String args[]) {
            try {           
                String[] cmd = { "ping.exe", "time.nist.gov" }; // Command and argument
                Process process = Runtime.getRuntime().exec(cmd); // Execute command
                // Read output - in separate thread
                streamReader sr = new streamReader( process.getInputStream() );
                sr.start();
                process.waitFor(); // Wait until process has terminated
            } catch (Exception e) {
                System.err.println(e);
    class streamReader extends Thread { // Read input stream (new thread)
        InputStream is;
        streamReader(InputStream is) {
            this.is = is;
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String rline;
                while ( (rline = br.readLine()) != null)
                    System.out.println(rline);   
                } catch (IOException e) {
                    System.err.println(e); 

  • Is it possible to call ms-dos command in abap program?

    Hi,
    is it possible to call ms-dos command in abap program?
    Thanks.

    Hi Cemil,
    You probably have your answer here:
    [Re: DOS/Windows command in app server;
    You create your external command with SM69 (you can test it with SM49).
    Then you call this command with function module "SXPG_COMMAND_EXECUTE".
    (See function group SXPT for all the calls to external commands).
    Regards,
    Thomas

  • How to run multiple DOS commands from a single Webutil Client_Host session?

    Hello all,
    I have a requirement where I need to create an interface with SVN from Forms for basic checkin-checkout of files.
    So, I've been trying to use webutil client_host to open a command line session and issue svn commands.
    For svn, sometimes I need to give multiple commands like change to a particular directory and then run an svn command.
    But client_host takes in only one command at a time and I'm unable to issue a series of DOS commands to perform
    a particular task.
    Is there a way to do this?
    Pls suggest.
    Regards,
    Sam

    First your original question... You can put more than one DOS command on a single line, simply separate each command with an ampersand (&). For example:
    mkdir c:\abc & cd abc & dir*
    Regarding your concerns about performance, well that would depend on exactly what you mean. Using CLIENT_HOST (or HOST on the server) simply opens a shell (DOS in this case) then passes your command to it. The performance of performing this action really isn't measurable. Basically you are just pressing a button and you should get a near immediate action. As for the performance of executing each command, that has nothing to do with Forms. Once the command is passed to the shell, the rest is a function of the shell and whatever command you passed.
    Having said that, if you were to write something sloppy like a loop (in pl/sql) which called CLIENT_HOST lots of times repeatedly, then yes there would be a performance problem because the pushing of the button will cause an exchange to and from the server and each cycle in the loop will do the same.
    So the answer to how performance is impacted will depend on what exactly you need to accomplish. If it is a single call to CLIENT_HOST, this should be fine.

  • Is there a way to run dos commands on onther system?

    Hi I have connected to a port of another system where windows is the operating system. So can I run dos commands on the other system through that network connection from my computer?

    I'm not sure what you mean by "connected to a port of another system" . Which port using what software to which server?

  • Execute DOS command in current window!

    All:
    Here is my code :
    import java.io.*;
    public class BuildScript {
    public static void main ( String[] args ) {
    String[] command = {"C:\\winnt\\system32\\cmd.exe", "cls"};
    try {
    Process process = Runtime.getRuntime().exec ( command );
    process.waitFor();
    } catch ( InterruptedException e ) {
    e.printStackTrace();
    System.exit(-1);
    } catch ( IOException e ) {
    e.printStackTrace();
    System.exit(-1);
    System.out.println ( "DONE!" );
    I just want to exeute some simple DOS command from Java application. And then I run this code, it hangs there for ever. I comments out "process.waitFor()", then it print out the "DONE" however it seems that it just open another console and clear that one and exit. For the main console I start my code, nothing happened.
    Can anyone help me out how to execute the dos command in current console window.
    Thanks

    Can anyone help me out how to execute the dos command in current console window.Sorry to say but there is no way to do that.
    The best way to clear the screen is to print a few hundred newlines.
    Trust me, this is a frequently asked question.

  • Is it possible to run dos-command's in java?

    I have a small security problem. I have a dos-based java prog. with login at the start og the program, but I have problems hiding the password. I have managed to hide the password when it is typed, but when you press enter and get to the next menu, it is only to press the up-key, and dos recover the last typed sentence. (The password...) I have been able to fine the dos-command "doskey /reinstall", which will clear the memory, but I haven't been able to implemet this in my java-prog.
    Any tips/trics are appreciated.

    I have a small security problem. I have a dos-based
    java prog. with login at the start og the program, but
    I have problems hiding the password. I have managed to
    hide the password when it is typed, but when you press
    enter and get to the next menu, it is only to press
    the up-key, and dos recover the last typed sentence.
    (The password...) I have been able to fine the
    dos-command "doskey /reinstall", which will clear the
    memory, but I haven't been able to implemet this in my
    java-prog.
    I doubt that calling that in your program would help.
    doskey is specific to the process. Running it by any means in java means that a new process will exist. You can verify this by open two console windows and running doskey in both. Nothing that you do in one will be reflected in the other.
    I don't think your problem is a program problem. That means that it should not be solved by software but rather by management. For example you certainly wouldn't expect your program to make sure that someone wasn't standing behind the user when they typed their password right? And what if someone installs a key grabber on the computer? In that case any program, yours included, would have all the key presses logged to a file.
    Alternatively if you might be able to solve it with one of the following...
    - Recode the application to use a GUI. If there is no console the doskey isn't going to get anything.
    - Write some JNI that bypasses the java input handling. If you handle the input directly then you can turn off echo
    - Inspect the java source code for the VM. There might be a hidden method to turn echo off. Alternatively you can find the code responsible for this, modify them, and use them with the bootclass option to replace the ones in the VM. This solution is not distributable. And it might not be possible solely using java.

  • How to execute dos command in Java program?

    In Perl, it has System(command) to call dos command.
    Does java have this thing?
    or any other way to execute dos command in java program?
    Because i must call javacto compile a file when the user inputed a java file name.

    Look in the Runtime class, it is implemented using the Singleton design pattern so you have to use the static method getRuntime to get its only instance, on that instance you can invoke methods like
    exec(String command) that will execute that command in a dos shell.
    Regards,
    Gerrit.

  • How to execute DOS command in Java?

    I want to execute a dos command in Java,such as execute test.bat command? How to realize it?
    Thanks in advance!

    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    I found this article really useful in solving this. Hope it helps.
    Cheers,
    John

  • Error while running DOS command in Java

    Hi all,
    I use "runtime" class to execute a DOS command "copy C:\\pathname\\*.* C:\\backupPathname\\". But I saw an error msg "CreateProcess: copy C:\pathname\*.* C:\backupPathname\ error=2".
    I did create a folder C:\backupPathname\ before running the DOS command.
    Pls help me all Java genius.

    I ended up writing a small 'c' program and pasing the command in to it from java. The problem seems to be that in dos, unlike unix, the copy command is not a program, rather the command is interpreted by the command.com program.

  • Does MAC OS have a "route add" command similar to a DOS command?

    Does MAC OS have a "route add" command similar to a DOS command?
    I know that with DOS, you can type "route add 192.168.x.x MASK 255.255.x.x 192.168.x.x" to add a route on your PC,but does MAS OS X(10.4.9) have something similar to it? I have a network printer that my winXP can access but my MAC cannot. I was unable to ping it from both wINXP and MAC. But after I added the route to my winXP I was able to ping and print to it. I do not know if I can do the same to my MAC. Does anyone know what the command is?
    Thank you in advance.

    Yes, and Welcome! ...
    http://www.osxfaq.com/man/8/route.ws
    And to make it permanent...
    http://www.osxfaq.com/tips/kluskens/index.ws

Maybe you are looking for

  • Runtime error : Time out

    Hi experts... I am trying to execute one program in production.. its giving me runtime error timed out.... I am giving you line on which it is showing runtime error... Please check it n suggest me how can we optimize this code to avoid the same error

  • PHP drop Down menu

    hi Everyone, my turn for help- I have inherited a web project from another person and have an issue regarding a drop down list that is tied in some way to another drop down list, the 2 are side by side as seen here > http://www.srf.org/preview/ < at

  • Good tutorial for using the orielly file upload servlet

    Hello all, I just downloaded the packages from the orielly site and I am interested in using the upload servlet. I have the oreilly servlet book but it doesnt get into much detail about how to use the servlet. I have not used servlets before so I nee

  • Spam error

    hello experts,            error while importing packages.            Error in phase : “DISSAMBLE”             Reason of error: CANNOT_CREATE_COFILE.             Return code: 8 any suggestions thnx & regards

  • Billing Error when trying to renew subscription

    Hey there, So I recieved an email notifiying my that my subscription has been suspeneded due to the inability to bill the credit card on file. First off, nothing has changed with the credit card and everything should be fine to bill so this should no