Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default exec problem, compiles but no output?

    My code works when I use notepad.exe or calc.exe but when I try to put in these other file names in their place I get a compile error that says it can't find the error. When I try to use a direct path, which is in the code below, it compiles but nothing happens.

    I'm stumped... Any thoughts or suggestions would be appreciated.

    Java Code:
    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();
                  }
            }
        }
    }
    Does anyone see what I'm missing? I've tried different variations of the file path and I can't figure it out. Google has not been able to suffice.
    Last edited by Dark; 04-16-2011 at 06:31 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You get a compile error? Could you paste that please?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Dark View Post
    Java Code:
            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
                {
                     p=r.exec(runPrograms[i]);
                     Thread.sleep(timeProgress[i]);
                      ...
    The array is named 'timePrograms', not 'timeProgress'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @JosAH Yeah that's not an error in my code. Its just a typo when I tried to copy it over to here. I program on my personal computer, and the computer I have internet access on are not the same. Its a government computer and I can't transfer files between the two.

    @ra4king I don't get a compile error. The code I posted is the one that compiles just fine but doesn't open any programs. If I change all the paths to notepad.exe or calc.exe it works perfectly.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @pbrockway2 I read the article but it didn't help me fix my problem. I'm trying to figure out why it won't open up the files.

    I've tried placing the jar in the folder Startup folder. I'm going to try putting it in the folder with the individual file now. That still won't fix my problem, but it will tell me a little bit more about my problem.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you don't get the error code when running a cmd in Runtime.getRuntime(), the only way I found out my error-message was by running the command myself in Windows cmd.exe


    assign your Runtime code to a Process like this:
    java.lang.Process p = Runtime.getRuntime().exec(command);

    and then make the thread wait until the process is completed:
    p.waitFor();

    also p.getErrorStream() should give you the error message


    and you will need to add:
    catch (InterruptedException ex) { }
    Last edited by ozzyman; 04-16-2011 at 05:03 PM.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @ozzyman Ok, so assign it to a process. I understand that part, but what does p.waitFor(); do? I'm not seeing how I'm supposed to open up the files with what you said.

    So when I make it only run Apache.bat and place the jar in the same folder as Apache.bat it sortof runs it. It shows up in my processes but it doesn't open up the command prompt showing that the bat is working. It does the same thing for ventrilo_srv.exe. However whenever I run the code I listed above it doesn't even open up the files into my processes. Ventrilo_srv actually gives me an error log.

    20110416 19:43:14 Unable to open ocnfiguration file 'ventrilo_srv.ini'.
    20110416 19:43:14 ERROR: Unable to read configuration data. Exiting.

    EDIT: I'm looking at the API and p.waitFor(); is saying that it waits until the process is terminated? Does this mean in a loop it would wait for the program to terminate before running through the loop again?
    Last edited by Dark; 04-16-2011 at 05:07 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    waitFor() causes the thread to wait if necessary until the process is terminated, in my case it helped me because the program was moving on before the process was completed (i was unlocking files though - and they weren't getting deleted after unlocking because they weren't getting unlocked in time)

    I don't think exec opens command prompt? is it supposed to?? i hope not but i can't tell cause my PC is latest spec material

    To be honest, your problem is more CMD than Java here. I think there could be an issue arising when you have such a long path to file also. There is another way to write the path with '~', it might be useful to check that out but in all honesty I don't know.

    Eg:

    replace /MaNGOS-Fun-Server-Repack-1.20/
    with /MaNGOS~1/ (cmd 8-character version)
    Last edited by ozzyman; 04-16-2011 at 05:17 PM.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm not sure. The .bat file opens up in a command window. When I run the file normally it opens up just fine.

    I guess I don't understand exec properly. I thought all the windows would open up like they do when you double click the file. Kind of like when the path is notepad.exe, it just opens up the notepad editor. That was the desired effect I was going for. I'll look into the file path length to see if thats a problem.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Sorry, opening bat files Should open a DOS window. Running any old command like changing a file attrib "attrib -h path" doesn't. .exec() in our case just runs a command in cmd.exe because we use Windows, but I don't know about other OS.

    My advice would be, make sure it works in CMD.exe, then it should work in your java application with .exec(), so long as you have security permission

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Is there a way to make a .Jar run as administrator? I was thinking that there might be a problem with rights in Windows 7.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    this works for me:

    Java Code:
    import java.lang.*;
    import java.io.*;
    
    public class LoadFiles
    {
        public static void main(String args[]) throws IOException
        {
            Runtime r = Runtime.getRuntime();
            Process p;
            try
            {
                p = r.exec("C:\\Windows\\notepad.exe");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    this also works:

    Java Code:
    import java.lang.*;
    import java.io.*;
    
    public class LoadFiles
    {
        public static String[] arrayFiles = new String[2];
    
        public static void main(String args[]) throws IOException
        {
            arrayFiles[0] = "C:\\Windows\\notepad.exe";
            arrayFiles[1] = "C:\\Windows\\System32\\mspaint.exe";
    
            Runtime r = Runtime.getRuntime();
            Process p;
            try
            {
                for (int i=0; i<arrayFiles.length; i++) {
                    p = r.exec(arrayFiles[i]);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    so i think its a problem with your paths

    EDIT:
    the other thing is that i'm sure .BAT stands for BATCH as in batch-processing. so if you run a .bat file, i'm not sure the program comes back until the batch processing is done
    Last edited by ozzyman; 04-16-2011 at 05:49 PM.

  15. #15
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I've tried it with forward slashes and backslashes but it won't load the five files I have in my code.

    With notepad, it works. I have no idea why...
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  16. #16
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I tried to run CPU-Z and I got this error:
    "The requested operation requires elevation"

    which I think means not enough privelages

  17. #17
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, now a great test would be to set the Jar to run as an administrator if thats even possible. I'll look into that now. Right clicking the jar file isn't allowing me to set it to run as administrator.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  18. #18
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    try making it an executable jar first?

    EDIT:

    thats not it, i have an executable jar and right-clicking it doesnt have the option to Run As..

    EDIT:

    There is however, if you click on Properties, a 'security' tab where you can click an 'Advanced' button to set the program security permissions. Try giving it 'full control' over everything
    Last edited by ozzyman; 04-16-2011 at 06:14 PM.

  19. #19
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    It has full control by default -.- This is a brainache.

    However I solved the problem with not opening a .bat I set the string for the bats to "cmd /c start C:\\Users\\Dark\\Desktop\\Startup\\MaNGOS-Fun-Server-Repack-1.20\\Server\\Apache.bat"

    Now it opens, however its giving me an error saying "Apache could not be started." Same for MySQL...
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  20. #20
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Did you test other executables? Try Microsoft Office Word, Internet Explorer, other browsers.

    I tested it with a handful of executables, including Apache, and they all worked for me.

Page 1 of 2 12 LastLast

Similar Threads

  1. Slow Output reading with Runtime.getRuntime().exec()
    By gmcouto in forum Advanced Java
    Replies: 8
    Last Post: 05-14-2010, 11:42 AM
  2. problem with exec(), subprocess starts after main process
    By krsnik02.abel@gmail.com in forum New To Java
    Replies: 1
    Last Post: 01-21-2010, 12:28 PM
  3. Problem with Runtime.getRuntime().exec with Linux Commands
    By swapnilnawale in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-23-2009, 11:23 PM
  4. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 02:35 AM
  5. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •