How to evoke a DOS console in Java?

Hello There,
I am trying to open a DOS console (says, cmd.exe in win2000) in Java. Obviously, the usual Runtime.getRuntime.exec("cmd") just does not work. Could someone please help me in solving this puzzle.
Many thanks,
Craig

What exactly do you want to do with the console once it is opened? The Runtime.getRuntime().exec("cmd.exe") does open a command processor. However, it will terminate when the JVM terminates.
Also, the JVM grabs hold of both the stdin and stdout handles that the console uses so there is no way for the user to interact directly with the command processor.
If you want to open a command processor that becomes completely separate from the JVM that launched it, do the following:
Runtime.getRuntime().exec("cmd.exe /c start");This will cause a command processor to be created by the JVM. The created command processor will start another command processor. The first command processor will terminate, but the second one will remain active without any ties to the JVM.
Hope this helps,
-Mike

Similar Messages

  • How to clear the DOS screen through java program

    how to clear the DOS screen through java program

    Just some Friday fun. Use the telnet program that comes with Windows and supports VT escape sequences.
    import java.io.*;
    import java.net.*;
    public class AutoTelnet {
         private static Socket s;
         public static void main(String[] args) throws Exception {
              Thread t = new Thread() {
                   @Override public void run() {
                        try {
                             s = new ServerSocket(5555).accept();
                        } catch (IOException ex) {
                             ex.printStackTrace();
              t.start();
              Process p = new ProcessBuilder("cmd", "/C", "start", "telnet", "127.0.0.1", "5555").redirectErrorStream(true).start();
              t.join();
              PrintStream ps = new PrintStream(s.getOutputStream());
              ps.println("Screen will be cleared in 5 seconds");
              ps.println("5");
              Thread.sleep(1000);
              ps.println("4");
              Thread.sleep(1000);
              ps.println("3");
              Thread.sleep(1000);
              ps.println("2");
              Thread.sleep(1000);
              ps.println("1");
              Thread.sleep(1000);
              ps.println("\u001b[2J");
              Thread.sleep(5000);
    }

  • HOW TO PRINTLN INTO DOS CONSOLE ?

    hi all,
    of course you can make outputs with System.out.println("Bla"); into the JBuilder console (for example).
    but what if i started my java application with a batch file?
    the batch file opens a dos console/window.
    how can i force the System.out.println of my code to appear into this dos window?
    thanx a lot,
    andi

    that's what i was thinking......the start.bat is like that:
    C:\jbuilder5\jdk1.3\bin\javaw -classpath "D:\Entwicklung\Java\Editor\classes;D:\Entwicklung\Java\Packages\Packages\classes;D:\Entwicklung\Java\Packages\xdogawt\CLASSES;D:\Entwicklung\Java\Packages\3rdParty\dog\dog\CLASSES;C:\jbuilder5\lib\xerces.jar;D:\Entwicklung\Java\Editor\src\Auguro.jar;D:\Entwicklung\Java\Packages\3rdParty\logkit-1.0b5.jar;C:\jbuilder5\jdk1.3\demo\jfc\Java2D\Java2Demo.jar;C:\jbuilder5\jdk1.3\jre\lib\i18n.jar;C:\jbuilder5\jdk1.3\jre\lib\jaws.jar;C:\jbuilder5\jdk1.3\jre\lib\rt.jar;C:\jbuilder5\jdk1.3\jre\lib\sunrsasign.jar;C:\jbuilder5\jdk1.3\lib\dt.jar;C:\jbuilder5\jdk1.3\lib\tools.jar" ASLEditor
    and this is EXACTLY what appears into my dos window....any system.out.print from the code won't appear.....
    andi

  • HOW DO I RUN DOS COMMANDS ON JAVA

    I SWEAR, I'LL PAY YA IF YOU HELP ME!!!
    Hi, this is the thing:
    have you ever run the "time" command on a DOS console?? if you have, you know that it shows the current time, and lets you set a new time.
    well, i need to make a java program to open a DOS console and execute the command, and making it able to write in information on the console so it sets a new time.
    MY PURPOSE IS NOT JUST WATCHING THE CURRENT TIME!! so please don't tell me to use System.getCurrentTimeMillis() or something like that; i explicitly need to run that DOS command. what's the Java code to do it??
    thank you!! and please attach your account # so you get a $50 deposit by the end of the week
    thank you!!

    Thanks a lot, you all guys, but i don't know what's with this thing.... it always throws an IOException, with the following exception stack trace:
    java.io.IOException: CreateProcess: temp.bat error=0
            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 Tarea.main(Tarea.java:15)and it will come out the same shit over and over. The above case was thrown by the line Runtime.getRuntime().exec("temp.bat") where temp.bat is a file where the only thing written in it is the word "time". and you can change the string parameter of the method exec, and thats what will change in the stack trace above.
    I also tried "cmd", "command", and "command.com". With the last one, it opens the command.com application but it freezes and does nothing. With the other two, appears the same old shit from above.
    Please help me!!
    Thank you...

  • How to execute MS DOS command through Java program???

    Dear Sir,
    I want to run a MS-DOS command through my Java program. I have tried "Dir" command but no other command which takes command line args doesn't work at all. Please help.
    import java.io.*;
    class CommandPrompt
         public static void main(String[] args)
              try
                   File file = new File("C:/Temp/Java");
                   String[] cmd = {"command.com","/c","md folder"};
                   String[] envp = {""};
                   Process m;
                   String s = "";
                   m = Runtime.getRuntime().exec(cmd,null,file);
                   BufferedReader buf = new BufferedReader(new InputStreamReader(m.getInputStream()));
                   while ((s = buf.readLine())!=null)
                        System.out.println(s);
              catch (Exception ex)
                   System.out.println("Exception is "+ex);
                   ex.printStackTrace();

    1. This forum is for Swing-related issues only. This question should be posted to the "Java Programming" forum.
    2. Please enclose your sample code in code blocks; it's much easier to read that way. See here for how it's done: http://forum.java.sun.com/faq.jsp#messageformat
    3. Please provide more information, like what error messages you got and what OS you're running. For instance, if you're running WinXP, Win2k or NT4, your command processor should be "cmd.exe", not "command.com". When I made that change, your program worked for me.

  • 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 execute java without a DOS console?

    I'm running java in the Windows platform.
    Every time when I run java program, there must be a
    DOS window.
    Is there any way that I can run java program without a DOS console window.

    Check out "executable jar files" - http://java.sun.com/docs/books/tutorial/jar/basics/run.html

  • 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

  • How to run DOS command in Java environment?

    Can i run DOS command in Java environment?
    I did like this:
    Runtime r = Runtime.getRuntime();
    r.exec("cmd.exe");
    r.exec("set classpath=%CLASSPATH%;.\\tmp")
    but failed.
    However if I run the java command, it runs successfully.
    r.exec("javac Test.java");
    r.exec("java Test");
    how should I do so that i can run the DOS commands metioned above in Java Environment?
    thanks a lot.

    Have a look at http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    This may help. I wonder if this is ok ?
    Runtime r= Runtime.getRuntime();
    r.exec("cmd.exe /C set classpath=%CLASSPATH%;.\\tmp\"");

  • How can DTExec show the execution package progress while running it in the DOS console?

    [SSIS 2012]
    Hi,
    We need to view the package results/progress during the execution of the package by the DTExec command. Isn´t it possible?
    The user can´t view the details for the execution by the GUI.
    The package is deployed in the SSIDB (Catalog). The final idea is to Audit the result in a *.log file from the Batch we have created. We are not considering to use the "Logging" feature while designing the package. We expect to have same SSIS events
    in the DOS console while executing it.
    DTExec always says: To view the details for the execution, right-click on the Integration Services Catalog, and open the [All Executions] report
    How could it be possible?
    Thanks,
    Alex Berenguer

    Hi Alex,
    I don’t think it is possible to display the package execution details in the Command Prompt when we execute a package stored in the SSISDB catalog. Except for enabling the package logging, we have to view the execution details from the “All Executions” Standard
    Report for the SSISDB Catalog.
    Regards,
    Mike Yin
    TechNet Community Support

  • How to execute a DOS PROGRAM as batch file  in java

    How to execute a DOS PROGRAM as batch file in java
    pls help me

    www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • 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 invoke dos shell from java program

    Hi,
    I'm not able to invoke dos shell from java.
    Can any one help me in this issue.
    I'm providing the source code below:
    try{
    Runtime.getRuntime().exec("cmd.exe")
    catch(IOException e) {
    System.out.println(e.getStackTrace());
    Thanks

    Does it throw a different exception?
    Or does it just do nothing at all?
    It does nothing at all[/b
    Is this a standalone Java app?
    Or a Java Applet running via a webbrowser? [b]It's a standalone application

  • Clear DOS Console

    Hi, I work with java application. I use jdk to compile and execute.
    When I execute this application, it's writes a DOS console. I use a System.out to print string in the DOS Console.
    I try to clean DOS Console.
    please, i don't know how to clean DOS Console.
    thanks.

    AFAIK, you can't send shell commands to your active shell window. So, you can either create a GUI with Swing components or something, or write like, 50 blank lines to System.out (HACK... COUGH).
    Jason

  • How to execute external  spawned process using  java

    Hi,
    I am executing PGP from command line using java.
    For adding the public key I use Runtime object like this
    Runtime rt = Runtime.getRuntime();
    Process process = rt.exec("pgp -ka "+"d:/Mangesh/pubkey/sandy.asc "+"C:/PROGRA~1/NETWOR~1/PGPNT/keyrings/pubring.pkr");PGP is executing this but inbetween it asked for confirmation "Do you want to add public key (y/n)"
    I am providing data to process this way:
    PrintWriter pw = new PrintWriter(new OutputStreamWriter( process.getOutputStream()));
    pw.write('y');
    pw.flush();But after this their is no execution from PGP and console remains until I closed it.
    PGP is waiting for ENTER KEY
    How can I provide ENTER KEY from java
    This is right method ??? or any other options are available??
    I am awaiting for your valuable suggestions .
    Regards
    Man479

    On Windows the "enter key" consists of two characters: carriage return and a line feed (\r\n). You may have to send both. If that doesn't work, pgp is not reading the reply through stdin, only through the console, and there's no way to pass the 'y' to it through Java.

Maybe you are looking for

  • Applet Not Working in Browser

    I built a java applet that works fine in the applet viewer, but just appears as a gray screen in a web browser. In the status bar of the web browser, it says that the applet has been started. The only possible cause of this that I can think of is a f

  • Problem to pass through firmware update... help needed !

    Hi there ! having trust in Apple has run software update on blind date basis, ie, just pressed the button to update everything what was proposed . i did not have a look, what was proposed to update, unfortunately ... apparently, some firmware update

  • Apple TV and Youtube Log in

    I was using Youtube on my Apple TV very easily for a year. Recently, I accidentaly logged out. When I try logging in it is giving me error for Username / Password incorrect. I tried several things including - Changing password - Log in using other Go

  • Tecra S2: check docking and automate Fn+F5

    Hi, I have a**Tecra S2 PM 730 1600 512MB 40GB DVD-CDRW 15,0"TFT WXPP*** Nvidia Chipset GO 6600 PN PTS20E-01J00LBT* TOSHIBA PORT REPLICATOR 3** PN PA3314E-1PRP -* s/n 34032428 ******** Tecra S2: Centrino PM730/XP Pro/15.0 ******** Agreement: FE065C7F

  • Uninstall iTunes 10.4

    How do I uninstall iTunes 10.4 and install 10.3 in Lion to enable FrontRow access to iTunes library? thx in advance