0

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";
        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();
              }
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Brandon
  • 13
  • 1
  • 6
  • 2
    Is this really the code you ran? I don't think this should even compile. The `catch` keyword needs to be followed by the type of exception you are catching. – Simon Nickerson Apr 16 '11 at 16:12
  • It does indeed compile without a single groan. It also runs perfectly when I set runPrograms[] to notepad.exe Like I previously stated in my original post. – Brandon Apr 16 '11 at 16:16
  • 2
    @Brandon F:\projects\numbered\all\222LoadFiles\LoadFiles.java:1: `';' expected` import java.lang.* ^ F:\projects\numbered\all\222LoadFiles\LoadFiles.java:2: `';' expected` import java.io.* ^ F:\projects\numbered\all\222LoadFiles\LoadFiles.java:30: `'(' expected` catch ^ 3 errors – Andrew Thompson Apr 16 '11 at 16:25
  • For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). – Andrew Thompson Apr 16 '11 at 16:26
  • no, the code does not compile. What you posted is not what you are running. – sbridges Apr 16 '11 at 16:26
  • I apologize, I added p= in front of r.exec(runPrograms[i]); That's probably whats making it not compile for you. I can't transfer the actual file over due to limitations of the computer I have the code on and the computer that's connected to the internet. – Brandon Apr 16 '11 at 16:36

2 Answers2

1

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)
Community
  • 1
  • 1
mdma
  • 56,943
  • 12
  • 94
  • 128
  • Ok, would this stop ventrilo_srv.exe from running though? – Brandon Apr 16 '11 at 16:23
  • Just a guess: it may be confused by the spaces in the path? – Simon Nickerson Apr 16 '11 at 16:28
  • @mdma Following the instructions in the link you posted the .bat files now open up. However the .exe's still do not open. The .bat files are also having errors when opening. "Apache could not be started." "MySQL could not be started." However when I run it outside of my program, it starts up just fine. Could it be a permission problem? If so how could I fix it? @Simon Nickerson I thought that might be a problem too, however realmd.exe and mangosd.exe's paths have no spaces in the path. – Brandon Apr 16 '11 at 16:28
  • Also worth checking that the current working directory isn't an issue. (Does your batch file cope with being run from a different working directory to the directory it's in?) – Simon Nickerson Apr 16 '11 at 16:46
  • @Simon Nickerson I'm not sure. I didn't write the batch file. Looking at it, all it does is start apache.exe and leave the window open. If you close the window thats opened by the batch file it shuts down apache.exe. I'm not experienced with batch files so I wouldn't know how to tell. If you want I could post the batch file code here. – Brandon Apr 16 '11 at 16:57
  • It's easy enough to test. Open a command window, change directory to a random location, type in `C:/Users/Dark/Desktop/Startup/MaNGOS-Fun-Server-Repack-1.20/Server/Apache.bat` and see if it starts Apache. – Simon Nickerson Apr 16 '11 at 17:05
  • It opens up the window, but gives me the error "Apache could not be started." Does this mean the batch file can't be run via the method I'm attempting? – Brandon Apr 16 '11 at 17:16
  • That's correct. Please see my edit - you'll need to specify the working directory and you should also split the command line into image name (the exe) and argument to avoid problems with spaces in paths. – mdma Apr 16 '11 at 17:20
  • @mdma Ok, so reading the API I'm still lost as to what the command would look like. It doesn't explain it in a way I understand. I was trying to come up with snippet of how I thought that line would work. But even closely consulting the API I have no idea how to utilize that form of exec(); – Brandon Apr 16 '11 at 17:37
  • I was reading the javaworld document again, for the fourth or fifth time, and it had the line of code that looks like what you were talking about. `exec("cmd.exe","/C",args[0]);`Would cmd.exe be the name of the file, /C be the path to the file and I still don't know what args[0] would be. – Brandon Apr 16 '11 at 17:41
  • @mdma So according to your example to make ventrilo_srv.exe to run would be. `exec(new String[] ventrilo_srv, null, "C:\\users\\dark\\desktop\\startup\\ventriloserver\\ventrilo_srv.exe");` Am I even close with that? – Brandon Apr 16 '11 at 18:04
  • @mdma I tried running the batch file one and its giving me an error. So something I'm putting in isn't being liked.`r.exec(new String[] {"cmd.exe","/c","Apache"}, null, "C:\\users\\dark\desktop\\startup\\mangos-fun-server-repack-1.20\\serve\\apache.bat");` Its saying _The methoed exec(String[], String[], File) in the type Runtime is not applicable for the arguments (String[], null, String)_ I'm not exactly sure what I would need to change to make this work. – Brandon Apr 16 '11 at 18:37
1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Its pretty hard to copy paste across computers. Funny how that works. Transferring files also becomes a problem on a government computer. They don't have too much of this "free internet" stuff in Afghanistan. I've been reading that page actually, before your useless smartass "answer". If I wasn't having a problem understanding why, even after that article, then I wouldn't have posted my question. – Brandon Apr 16 '11 at 16:40
  • Good luck with that attitude problem. Also with the communication problem (e.g. we are not psychic, and cannot know that you read the page, especially when your code in no way reflects that). Oh, and also that communication problem where, if you cannot think of something intelligent to reply, you fall back to making obtuse comments about Afghanistan, and using swear words. – Andrew Thompson Apr 16 '11 at 17:27
  • I wish you well with your personal relations. You expect anyone to take anything you say without response. You resort to belittlement and demeaning forms of communication, and offering no worthwhile suggestions. Now please exit, seeing as you're useless in this situation. Personally, I would rather be stuck on this problem for weeks before attempting to listen to any answers you may have. Those who give no respect get none, and sir you surely have none from me. – Brandon Apr 16 '11 at 17:52
  • @Brandon "Personally, I would rather be stuck on this problem for weeks .." Your wish will most probably be granted. ;) "Those who give no respect get none, and sir you surely have none from me." I'm devastated. – Andrew Thompson Apr 16 '11 at 18:11
  • I'm sure your life is full of perfect relations with people. Criticizing attitude with an attitude of your own? Easily considered worse? Its hard to devastate something that's already been destroyed. Cya Troll. – Brandon Apr 16 '11 at 18:25