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.

Similar Messages

  • 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?

  • 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\"");

  • 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

  • Error while running the SAP JCO java program running via command line

    Hi,
        We are facing an issue while using SAP JCO. When i try to run the sample program using RAD 8.0 ( IBM IDE tool For Java Development) its working fine.
    The same sample program if i try to run using command line, Then its giving below exception message.
    Exception in thread "main" java.lang.NoClassDefFoundError: Integration
    NOTE: I have configured proper sapjco jar & Dll files path in class path settings in my batch file.

    Hi,
    class loader can't find class definition during runtime but it could find it during compilation. So the problem is with your classpath. Does your classpath point to file with class Integration? Check this [blog|http://javarevisited.blogspot.com.au/2011/06/noclassdeffounderror-exception-in.html].
    Cheers
    Added reference to blog.

  • Error while running executable file through java in WinNT

    I would like to run an executable file with Java.
    - If I try with notepad or paint, i.e. Windows Applications,
      I have no problem.
    - I also can run Non-Windows-Own Applications, except one.
      I get an error message, if I want to run this program through Java.
    I have tried following commands to run an executable file.
    Runtime.getRuntime().exec("cmd.exe /c "+command);
    Runtime.getRuntime().exec("cmd.exe /c start "+command);
    Runtime.getRuntime().exec("cmd.exe /c start /wait "+command);
    Runtime.getRuntime().exec("cmd.exe /k start "+command);
    command : the path to the executable file
    I can run the application directly, if I click the icon on desktop,
    but not through Java.
    here is the error message I get
    screenshot : http://www.aykut.de/error_message.jpg
    Text : "Security Check failure"
            The Logon System has been tampered with.
            The Administrator will need to re-install.
    my Idea :
    The application is "old".
    I think it was written for Win 3.1.
    Therefore I don't know if there is any other
    possibilty to run a "DOS Exe File" through Java.

    I have just figured out how it works,
    if somebody else here in forum have this problem,
    here is the solution :
    String path = "F:\...\...\Application.exe";
    String envDir = path.substring(0, path.lastIndexOf("\\"));
    String[] command = {"cmd.exe", "/c", "start", "/wait", "/D"+envDir, path};
    Process process = Runtime.getRuntime().exec(command);
    "start /Dpath" => path: environment directory F:\...\...\
    "start /wait" => wait until Application.exe terminates
    if you use Win95 or Win98 use command.com instead of cmd.exe
    Aykut

  • 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...

  • Error while running Snapshot Command

    We have Create a MDX Query in Model Designer to display Revenue and Demand Plan units from Demand cube.
    SELECT{ [FiscalCalendar].[FiscalCalendar]} ON COLUMNS,
    { CROSSJOIN({ [Product].[Product] },
    { [Measures].[Revenue Plan],
    [Measures].[Demand Plan Units] })}
    ON ROWS FROM [Demand] QUERY PROPERTIES flattenColumns=true
    and while running a query it is giving correct Output
    Row Count: 2
    Product Measures FiscalCalendar
    Product Revenue Plan $3,777,202,750.00
    Product Demand Plan Units 7,515,860
    On we Logging into Isadmin environment and run a snapshot command which gives error.
    Admin> snapshot data using query test (Our Query name)
    it does not work and gives error, please suggest a valid way to run this command.

    We ran into this, too. It was filed as a bug and will be fixed in the PS1 release (11.1.2.1). Oracle wouldn't commit to a release date tho. As this is core functionality, it would be good if you raised it with Oracle support. This will make it easier to lobby for a patch.
    Edited by: matt on Dec 29, 2010 4:23 PM

  • Running Dos command in Java

    hey, Just wondering can anyone help me get started, i pretty new to java and wanted to run the date command in Dos from a java application. If i could just know where to begin or even if this is possible, it would be much appreciated!!

    If you're using Runtime.exec() you need to read this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    If you're new to java it might be a bit heavy, but definitely add it to your "Favourites" for later!

  • How to run dos command on java

    would java run the dos command ?
    if yes ~would you mind give me some example~please~

    ..or just use:
    Runtime.getRuntime().exec("cmd /c commandyouwanttouse");
    Hope it works!                                                                                                                                                                                               

  • Running Dos Command using Java

    How do i run a particular Dos command using a simple java class?

    Process process = Runtime.getRuntime().exec( ... );
    Search the forum. There are plenty of examples.

  • Error while running the command "./biserverxmlgen"

    When we are trying to run "./biserverxmlgen" in the direcoty "cd /home/biadmin/obiee11g_home/Oracle_BI1/bifoundation/server/bin" we are getting the below error message.
    "error while loading shared libraries: libnqsclusterapi64.so: cannot open shared object file: No such file or directory".
    Please help us to get this resolved.

    ./biserverxmlgen: error while loading shared libraries: libnqsclusterapi64.

  • Error while running ComponentTool command line

    Hi ,
    I am getting following exception while running command line component tool in linux
    ./ComponentTool --list
    csPathUnableToFindParameter(IdcProductName,$IdcResourcesDir/core/config/defaultconfig-$IdcProductName.cfg)
    Please give some pointer to get out of this issue
    Thanks
    Hari
    Edited by: Hari on Apr 2, 2012 4:04 AM

    Hi Jonathan,
    configFileList value is ConfigFileList=$IdcResourcesDir/core/config/defaultconfig-$IdcProductName.cfg
    we have following files in core/config directory
    defaultconfig.cfg launcher-aix.cfg launcher-hpux64.cfg launcher-hpux-ia.cfg launcher-linux-s390.cfg launcher-solaris64.cfg launcher-solaris-x86.cfg
    defaultconfig-idccs.cfg launcher.cfg launcher-hpux.cfg launcher-linux64.cfg launcher-linux-s390x.cfg launcher-solaris-amd64.cfg launcher-win32.cfg
    launcher-aix64.cfg launcher-freebsd.cfg launcher-hpux-ia64.cfg launcher-linux.cfg launcher-osx.cfg launcher-solaris.cfg launcher-windows-amd64.cfg
    Thanks
    Hari

  • Getting errors while running cpio command on software download

    i am trying to run cpio command on BPEL Process Manager (10.1.2.0.2) downloaded software and getting below error.
    $ cpio -icdmv < as_hpux_parisc_integration_101202_disk1.cpio
    Out of phase--get help
    Perhaps the "-c" option shouldn't be used
    i tryed without the c option
    $ cpio -idmv < as_hpux_parisc_integration_101202_disk1.cpio
    Out of phase--get help
    Perhaps the "-c" option should be used
    I download 5 times and had problems all the time .this is on HP 11.23. I tryed with rest of the softwares from oracle site and I am successful

    no luck
    $ cpio -idv < as_hpux_parisc_integration_101202_disk1.cpio
    Out of phase--get help
    Perhaps the "-c" option should be used

Maybe you are looking for

  • B1BC - Dashboard de Consolidação

    Prezados Boa tarde, Iniciei a utilização do B1BC e cheguei até o ponto de consolidar Lançamentos Contábeis, consegui exportar e importar o plano de de contas e associar as contas, porém no momento de exportar lançamentos já tem 10 dias que "está proc

  • More than one LocalDevice with Java Bluetooth API

    Hi.. If I have for example 2 usb bluetooth dongle, is it possible to use both of them with my java program?

  • Devices Disconnecting From Home Hub 5

    Had this problem ever since changeing to the home hub 5 / infinity a couple of weeks ago. Devices such as my laptop and the sky wireless adapter (for getting on demand etc) keep disconnecting from the home hub and i keep having to reconnect them. Its

  • Production Order for variant subassemblies

    Hi all, I have a scenerio were there is header material called A. It has sub-assemblies C,D,E. Each subassemblies have lot of variations. So the User does not want to create individual Material Master for these variations. But the user wants to book

  • I need a super loud ringtone for my Nokia 808.

    My wife's 808 uses "Nostalgia", which I like. But I want something different so I can tell which phone is ringing. I want a ringing sound like "Topic" or "Nostalgia". But "Topic" is not loud enough.