0

Im trying to run a bat from C:/abc/def/coolBat.bat but my java workspace is in D:/
I've tried with :

String cmd = "cmd /c /start C:/abc/def/coolBat.bat";
Runtime.getRuntime().exec(cmd);

But didn't work, so I tried this

String[] command = { "cmd.exe", "/C", "C:/abc/def/coolBat.bat" };
Runtime.getRuntime().exec(cmd);

didnt work either. Tried this too

Executor exec = new DefaultExecutor();
exec.setWorkingDirectory(new File("C:/abc/def"));
CommandLine cl = new CommandLine("coolBat.bat");
int exitvalue = exec.execute(cl);

Says it cant find the file.

Tried something like this too:

Runtime.getRuntime().exec("cmd cd /d C:/abc/def/ && coolBat.bat");

And nothing. The weird thing is that this command:

cd /d C:/abc/def/ && coolBat.bat

Works when i do it in cmd. Its worth saying that the bat file copies some files to another directory, all inside C:/

EDITED N°1

CD C:\abc\def\MN
copy almn + ctmn + bamn C:\abc\def\mn_sf.txt
CD C:\abc\def\ME
copy alme + ctme + bame  C:\abc\def\me_sf.txt
CD C:\abc\def\
if exist MN.txt del MN.txt
if exist ME.txt del ME.txt
if exist JUZ.txt del JUZ.txt
if exist FUNC.txt del FUNC.txt
if exist AHO.txt del AHO.txt
CD C:\
Mokz
  • 124
  • 1
  • 16
  • Runtime.getRuntime().exec("cmd /c C:\abc\def\coolBat.bat"); - should work. What error are you getting? – access_granted Jun 05 '17 at 21:55
  • @access_granted thats the thing. Im not getting any error when I run it. But it doesnt do anything. My workspace is in D:/ disk, the bat is in C:/abc/def/coolBat.bat and it copies files within the C:/ disk – Mokz Jun 05 '17 at 22:03
  • Try adding the following lines into your bat file: c:\; cd c:\abc\def; echo ok >>test.txt; - and see if it creates the test file in principle. Can you also post the contents of your bat here as well? – access_granted Jun 05 '17 at 22:12
  • @access_granted I can't modify the bat file. I've edited my question with the content of the bat file – Mokz Jun 05 '17 at 22:23
  • added a working Java code – access_granted Jun 05 '17 at 23:38

4 Answers4

0

Allow MS Windows to use the associated application to run your batch file (or any other application):

Required Imports:

import java.awt.Desktop;

Here is code you can try:

String filePath = "C:/abc/def/coolBat.bat";

if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File(filePath);
        Desktop.getDesktop().open(myFile);
    } 
    catch (IOException | IllegalArgumentException ex) {
        System.err.println("Either there is no application found "
            + "which is associatd with\nthe file you want to work with or the "
            + "file doesn't exist!\n\n" + filePath);
    }
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • The bat executes, but is not doing what is supposed to do (copy files from one directory to another). I've tested the bat file and works fine when I do it manually – Mokz Jun 05 '17 at 21:53
  • Perhaps then try and use Windows RoboCopy or xcopy in your batch file. See [this SO post](https://stackoverflow.com/questions/986447/batch-file-to-copy-files-from-one-folder-to-another-folder). Or, just use Java to carry out the task. – DevilsHnd - 退職した Jun 05 '17 at 23:02
0

The Java version could work as:

String[] command = {"cmd.exe", "/C", "Start", "/D", "c:\\abc\\def", "c:\\abc\\def\\coolBat.bat"};

Process process = Runtime.getRuntime().exec(command);

    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((line = input.readLine()) != null) {
      System.out.println(line);
    }
    input.close();

    BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

    while ((line = errReader.readLine()) != null) {
        System.out.println(line);
    }

    System.out.flush();
    int retCode = process.waitFor();
    System.out.println("Return code: " + retCode);
access_granted
  • 1,807
  • 20
  • 25
  • Tried it. It doesn't show any error, but doesn't do what is supposed to do. I figured I had to change my workspace to C:/ and it worked. that /D works when done manually, but oddly enough, not with java apparently – Mokz Jun 06 '17 at 01:07
  • Add the following code to catch stderr and stdout to your class. – access_granted Jun 06 '17 at 17:15
0

Well I finally got it to work, just had to change my workspace to C:/

Apparently the problem was that it couldn't change from D:/ to C:/ to execute. I ran the same commands I tried before and there was no problem.

Guess the question remains, why it couldn't change from D:/ to C:/ when running commands from Java.

Thanks to everyone for the help

Mokz
  • 124
  • 1
  • 16
0

Try this:

String[] command = { "cmd.exe", "/C", "C: && C:/abc/def/coolBat.bat" };
Pang
  • 9,564
  • 146
  • 81
  • 122