开发者

exec() question, not opening files

So heres my problem, I'm trying to get my jar file to open up 5 files with time in between each file opening. When runPrograms[] is set to notepad.exe instead of the direct file path it will open up notepad just fine. However whenever I try to get it to open up the files that are currently in the code it doesn't accomplish anything. I tried placing the Jar in the folder Startup with the other folders and shortening the path and that didn't work.

When I manually open the files they all work, so theres no compatibility problems with the files. I believe my problem lies within the file paths. I've tried forward and backslashes but it still doesn't open any files.

import java.lang.*
import java.io.*

public class LoadFiles
{
    public static void main(String args[])throws IOException
    {
        Runtime r = Runtime.getRuntime();
        String[] runPrograms = new String[5];
        runPrograms[0] = "C:/Users/Dark/Desktop/Startup/MaNGOS-Fun-Server-Repack-1.20/Server/Apache.bat";
        runPrograms[1] = "C:/Users/Dark/Desktop/Startup/MaNGOS-Fun-Server-Repack-1.20/Server/MySQL.bat";
     开发者_JS百科   runPrograms[2] = "C:/Users/Dark/Desktop/Startup/MaNGOS-Fun-Server-Repack-1.20/MaNGOS-Server/realmd.exe";
        runPrograms[3] = "C:/Users/Dark/Desktop/Startup/MaNGOS-Fun-Server-Repack-1.20/MaNGOS-Server/mangosd.exe";
        runPrograms[4] = "C:/Users/Dark/Desktop/Startup/Ventrilo Server/ventrilo_srv.exe";

        int[] timePrograms = new int[5];
        timePrograms[0] = 5000;
        timePrograms[1] = 5000;
        timePrograms[2] = 5000;
        timePrograms[3] = 5000;
        timePrograms[4] = 5000;

        for(int i=0;i<5;i++)
        {
            try
            {
                 r.exec(runPrograms[i]);
                 Thread.sleep(timePrograms[i]);
             }
             catch
             {
                  System.out.println("Error==="+e.getMessage());
                  e.printStackTrace();
              }
        }
    }
}


Once you have sorted the problems with copy/paste (wonders: just how difficult can that be?), look at When Runtime.exec() won't. After you've looked at it, implement all the recommendations(1) of that document.

1) Even once you fix the code to compile, & the problem mentioned by mdma, the code will still have problems.


You can't run ".bat" files directly - unlike an "exe" they are not executable images. You use "cmd.exe" to run batch files, for example via the command line "cmd.exe /c myfile.bat".

See How do I run batch files from my java application?

EDIT: Since some of the paths contain spaces, and some of the processes may be dependent upon a working directory, you will be better off using:

exec(String[] cmd, String[] env, File dir)  ([javadoc][2])

At present, your command line is being parsed, so paths with spaces in them will be split into command and arguments. Using this form of exec avoids the parsing. Specifying the working directory may be required by some programs. At present, each child process gets the working directory of your parent process, which is probably not what these subprocesses require.

EDIT2: When you run a regular process (i.e. an exe), then call

exec(new String[] { filename }, null, workingDirectory) 

To launch a batch file, you are running cmd.exe passing two arguments, /C and your batch file. so

exec(new String[] { "cmd.exe", "/c", batchFile }, null, workingDirectory)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜