Running a .BAT file in JAVA without opening a command prompt

Simon T

I've been trying to run an executable .bat file in Java using the line:

Runtime.getRuntime().exec("call " + batFile);

But it returns an error

Could not start process with commandLine nullCreateProcess: call batfilename here error=2 IOException: Create Process: call batfilename here error=2

I managed to bypass this by replacing the String in the exec() function with "cmd /c start " + batFile but this opens a command prompt which is not allowed.

Are there workarounds to this? Thanks!

MadProgrammer

Try running the batch file directly, for example...

ProcessBuilder pb = new ProcessBuilder("C:/Test.bat");
pb.redirectError();
try {
    Process p = pb.start();
    try (InputStream inputStream = p.getInputStream()) {
        int in = -1;
        while ((in = inputStream.read()) != -1) {
            System.out.print((char)in);
        }
    }
    System.out.println("Exited with " + p.waitFor());
} catch (IOException | InterruptedException ex) {
    ex.printStackTrace();
}

This was the batch file...

@echo Hello World

(I know, massive) and the code outputted...

Hello World
Exited with 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

BAT file to map to network drive without running as admin

From Dev

JDBC Java File Running Through Command Prompt

From Dev

Put editable text in command prompt with bat file

From Dev

Running a .bat file in Java

From Dev

Running mysql command from bat file with redirection

From Dev

Stop command prompt from opening when running compiled .exe

From Dev

Glassfish asadmin batch file not opening command prompt

From Dev

for /f is not working in .bat file but it works with command prompt in windows 2003

From Dev

Running .class File in Command Prompt

From Dev

Difference between running Java from command line with file ending and without

From Dev

UnboundLocalError running .py from .bat (with API); Runs fine in Command Prompt

From Dev

Subprocess module not returning output when running command by opening cmd prompt

From Dev

Calling and executing vb script by opening command prompt through Batch file

From Dev

JDBC Java File Running Through Command Prompt

From Dev

Compiling and running a java program in command prompt

From Dev

Running a .BAT file in JAVA without opening a command prompt

From Dev

Command Prompt: java file not found

From Dev

Running shortcut from command prompt without the .lnk extension (Windows)

From Dev

Running Python in a command prompt

From Dev

Launch a bat file without a command window as administrator?

From Dev

Windows Command line: Is there any way to check syntax of bat file without actually running it?

From Dev

Running a .bat file as a command on Windows 10?

From Dev

Running .class File in Command Prompt

From Dev

Opening Cygwin with Windows bat file and running script file

From Dev

hide command displaying in command prompt while running batch file

From Dev

Opening a HTML file in SeaMonkey composer from the command prompt

From Dev

CMD prompt - bat file - Taskkill command ignores start /wait

From Dev

Errors When Running Java from Command Prompt

From Dev

Works in Command Prompt but not in BAT-file or CMD-file

Related Related

  1. 1

    BAT file to map to network drive without running as admin

  2. 2

    JDBC Java File Running Through Command Prompt

  3. 3

    Put editable text in command prompt with bat file

  4. 4

    Running a .bat file in Java

  5. 5

    Running mysql command from bat file with redirection

  6. 6

    Stop command prompt from opening when running compiled .exe

  7. 7

    Glassfish asadmin batch file not opening command prompt

  8. 8

    for /f is not working in .bat file but it works with command prompt in windows 2003

  9. 9

    Running .class File in Command Prompt

  10. 10

    Difference between running Java from command line with file ending and without

  11. 11

    UnboundLocalError running .py from .bat (with API); Runs fine in Command Prompt

  12. 12

    Subprocess module not returning output when running command by opening cmd prompt

  13. 13

    Calling and executing vb script by opening command prompt through Batch file

  14. 14

    JDBC Java File Running Through Command Prompt

  15. 15

    Compiling and running a java program in command prompt

  16. 16

    Running a .BAT file in JAVA without opening a command prompt

  17. 17

    Command Prompt: java file not found

  18. 18

    Running shortcut from command prompt without the .lnk extension (Windows)

  19. 19

    Running Python in a command prompt

  20. 20

    Launch a bat file without a command window as administrator?

  21. 21

    Windows Command line: Is there any way to check syntax of bat file without actually running it?

  22. 22

    Running a .bat file as a command on Windows 10?

  23. 23

    Running .class File in Command Prompt

  24. 24

    Opening Cygwin with Windows bat file and running script file

  25. 25

    hide command displaying in command prompt while running batch file

  26. 26

    Opening a HTML file in SeaMonkey composer from the command prompt

  27. 27

    CMD prompt - bat file - Taskkill command ignores start /wait

  28. 28

    Errors When Running Java from Command Prompt

  29. 29

    Works in Command Prompt but not in BAT-file or CMD-file

HotTag

Archive