Run .bat file from java?

I will come back with my previous problem when executing a .bat file from a java program.

When I execute my Java code, I don’t understand why it is looking for my .bat file in the project directory of my Eclipse.

I clearly declare the path as follows: "cmd.exe", "/C", "Start", "C:\\File\\batfile.bat" If someone can explain it clearly to me, please. Thank you very much!

I am using win xp and Eclipse Helios.

here is my code:

String cmd;
        try {
            String[] command = { "cmd.exe", "/C", "Start", "C:\\File\\batfile.bat" };
                Runtime r = Runtime.getRuntime();
                Process p = r.exec(command);
                p.waitFor();

                } catch (Exception e) 
                {

                System.out.println("Execution error");} 
+3
source share
4 answers

The cmd.exe process (selected from your PATH environment variable) is created with the current working directory in the same way as in the parent process (eclipse.exe = java). This is most likely c: \ eclipse or the workspace directory.

(C:\File\batfile.bat), . , " Java", . , BAT .

+1

:

String com = System.getEnv("ComSpec") != null ? System.getEnv("ComSpec") : "C:\\Windows\\System32\\cmd.exe";

String[] command = { com, ...... }

ComSpec cmd.exe. , ( ). % SystemRoot%\system32. % path%. ComSpec , cmd.exe, .

- , Eclipse Eclipse.

, , - . -, , ( ).

+1

Remove Startfrom the command - this is not necessary - and try:

String[] command = { "cmd.exe", "/C", "C:\\File\\batfile.bat" };
+1
source
Runtime.getRuntime().exec("cmd /c start C:\\File\\batfile.bat");
0
source

Source: https://habr.com/ru/post/1794105/


All Articles