views:

626

answers:

8

I wrote a batch file that reads in a text file which contains a list of file name, and then delete and rename them one by one. I did this by using a for loop, and it works perfectly by double clicking it.

But when I tried to call this batch from a Java program. It's no longer working for this part:

for /f %%a in (ListFile.txt) do (
    DEL %%a
    REN %%a_NEW %%~nxa
  )

If I only specify a certain file name. It's working by calling from Java. Say,

DEL tag.jar
REN tag.jar_NEW tag.jar

The same thing happen for 'call' command. It's not working if calling from Java program, which makes me have to use 'start' command.

Can someone tell me why is it like this? How can I make it also working if calling from Java program?

+1  A: 

How are you calling it from Java?

What exactly is happening when you do?

Why do you call a batch file instead of doing the same thing in Java, when you're calling it from Java anyway?

Michael Borgwardt
A: 

The Java code is

Runtime.getRuntime().exec("update.bat");

When called from Java, it's like nothing happened. I don't know how to debug the batch...

I want Java to call batch, because I need Java program to be turned off first, then I can update some jar files by using a different program. I tested the batch by running it alone, which is working, but when get called from Java program, it behavior differently...

Please help. Thanks.

Wait wait wait. You're calling the batch from Java and killing Java in the batch?
Michael Myers
A: 

Maybe the current directory is not what you think so the ListFile.txt is not found.

Try something like :

set BAT_HOME=%~dp0
echo %BAT_HOME%

for /f %%a in (%BAT_HOME%\ListFile.txt) do (
    DEL %%a
    REN %%a_NEW %%~nxa
  )
RealHowTo
A: 

Thanks for the input. %BAT_HOME% includes '\' already. So I removed one '\'. But it's still not working. Here is my complete little batch file. I think that errorlevel is also not working...

set BAT_HOME=%~dp0
find "updated already" "%BAT_HOME%ListFile.txt"  
if errorlevel 1 ( 
    for /f %%a in (%BAT_HOME%ListFile.txt) do (
            DEL %%a
            REN %%a_NEW %%~nxa
    )   
    echo updated already >> %BAT_HOME%ListFile.txt  
)

What I did is, to check if the txt file contains "updated already". If not, it will loop all files in txt file and append "updated already" at the end of it.

+1  A: 

You should use:

Runtime.getRuntime().exec("cmd /c update.bat");

Instead.

Here is a snippet on how to run programs from java.

And below how I test it.

import java.io.*;
public class RunIt {
    public static void main( String [] args ) throws Throwable   { 
        Process p = Runtime.getRuntime().exec( "cmd /c update.bat");
        InputStream i = p.getInputStream();
        for(  int c = 0 ; ( c =  i.read() ) > -1  ; ) {}
        i.close();
    }
}

For some reason that goes beyond my comprehension, if I don't read the input the process is not executed, that's the reason of the for loop above.

Here's my test:

C:\oreyes\samples\java\readinput>type update.bat
echo "x" >> x.txt

C:\oreyes\samples\java\readinput>dir x.txt
 Directorio de C:\oreyes\samples\java\readinput

 No se encuentra el archivo // That is file not found

C:\oreyes\samples\java\readinput>javac RunIt.java

C:\oreyes\samples\java\readinput>java RunIt

C:\oreyes\samples\java\readinput>dir x.txt

01/16/2009  11:14 AM                 6 x.txt // file created hence update.bat executed
               1 archivos              6 bytes

I hope this helps.

OscarRyz
A: 

Process p = Runtime.getRuntime().exec("update.bat");

p.waitFor();

l_39217_l
A: 

Are you sure you're allowed to exec a BAT script? I don't think Windows treats them as first-class executables in the same way as Unix does. You may have to do something weird like:

...exec("cmd /c update.bat")
Adrian Pronk
A: 

Thanks guys, I tried:

Runtime.getRuntime().exec("update.bat");
Runtime.getRuntime().exec("cmd /c update.bat");
Runtime.getRuntime().exec("cmd /c call update.bat");

But all above don't make difference to the execution of the batch file. It still does not go inside of

if errorlevel 1 ()

and does not execute DEL and REN inside

for /f %%a in (%BAT_HOME%ListFile.txt) do ()

`

I can not call p.waitFor(); because I want Java program to exit first, then to let batch run, since batch is used to change jar files used by Java program.

My Java version is 6.0

what version of java ?
l_39217_l