Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree5Likes

Thread: Running Programs through Java

  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Running Programs through Java

    I've been playing around with this for about an hour.

    Java Code:
    Runtime runTime = Runtime.getRuntime();
        try {
          Process process = runTime.exec("notepad");
        } catch (IOException e) {
          e.printStackTrace();
        }
    So that works. Notepad will open.



    However, I'm trying to get other programs to open. Specifically, this program: C:\Octave\3.2.4_gcc-4.4.0\bin\octave-3.2.4.exe

    However, using that in place of notepad doesn't work. I'm assuming that there is some sort of system variable that explains why simply typing "notepad" works? As if you type notepad into the run box, notepad will open. Soo does that snippet work by going through some sort of system variables?

    How would I go about opening other programs, such as the one I referenced above.

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    The OS uses the PATH variable to find a program to be executed.
    I don't know why giving the full path to the executable file doesn't work.

    Intercept the process's output and error streams and print them to see if there are any error messages.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    How would I go about catching the output and error streams?


    When I run the program, absolutely nothing happens. It doesn't throw an error. The whole program executes. Just nothing opens.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    The Process class has methods that will return references to those streams.

    The whole program executes. Just nothing opens.
    What is supposed to happen? What does the program open that is not being opened?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Yes, correct. The code is supposed to open the program C:\Octave\3.2.4_gcc-4.4.0\bin\octave-3.2.4.exe

    The program does NOT open.

    The rest of my code executes, however, if I happen to have something like print "hey" at the end, it'll print that.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    I'm confused by these statements:

    The program does NOT open.
    The whole program executes.
    What does it mean if the program executes and it does NOT open?

    What does "NOT open" mean? Not all programs have a GUI window.

    Programs can execute without having a window.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Sorry, this is due to my poor terminology.

    For the purpose of this explanation, "program" will mean Octave, the actual program I am referencing and trying to get to open in a new window.
    "script" will refer to the .java file while I am running.

    The "program" that I'm trying to open through my "script" does NOT open. However, the rest of the "script" executes.

    AKA:
    Java Code:
    Runtime runTime = Runtime.getRuntime();
        try {
          Process process = runTime.exec("octave");
        } catch (IOException e) {
          e.printStackTrace();
        }
    System.out.println("This happens");
    This will print out "This happens," but nothing else will happen. The linked program will NOT open.
    Last edited by AlexGraal; 03-30-2014 at 06:52 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    What does "open" mean? Do you mean start execution?
    If it doesn't start execution is that because:
    1) the OS can not find it
    2) the OS determines there is something wrong with the file and it can't be executed
    3)the program starts and immediately dies

    What is written to the output stream and the error stream when the exec() method is called?

    Is the path to the "octave" program in the PATH variable?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Ok, here we go.

    So, I added the location of my shortcut (to the octave program) to my PATH variable as follows.

    PATH = blahblahblah;C:\Python27\Scripts\;C:\ProgramData\M icrosoft\Windows\Start Menu\Programs\GNU Octave 3.2.4

    "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\GNU Octave 3.2.4" contains a SHORTCUT to the actual octave program. The reason I don't want to use the folder for the octave program itself is because the program's folder has a ton of other files in it (dll files) with more or less common seeming names, so I don't want to cause any issues.


    Then I tried doing this:
    Java Code:
    Runtime runTime = Runtime.getRuntime();
    Process process = runTime.exec("Octave.lnk");
    This is the error that I got:
    "java.io.IOException: Cannot run program "Octave.lnk": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at openClass.openClass(openClass.java:73)
    ..... much more down here, another 40 or so lines, but useless for you to see them all"

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    The system cannot find the file specified
    That says the system can't find the file. I don't know what using a shortcut does.

    Try a batch file.
    Last edited by Norm; 03-31-2014 at 01:50 AM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Running Programs through Java

    Iirc Windows shortcut is not like Unix/Linux symbolic link. You can't execute a shortcut via command line, and so I don't think adding the path to the shortcut will help.

    The reason I don't want to use the folder for the octave program itself is because the program's folder has a ton of other files in it (dll files) with more or less common seeming names, so I don't want to cause any issues.
    It doesn't matter much what the directory/folder with the executable file contains. Windows will just go through all the directories listed in PATH, and check if the filename you typed matches any of the executable files in the directories. It won't try to execute non-executable files such as .dll files as Windows determines file type through the filename extension.

    Try
    Java Code:
    Process process = runTime.exec("C:\\Octave\\3.2.4_gcc-4.4.0\\bin\\octave-3.2.4.exe");
    (I'm assuming that's the correct absolute path and filename. If not, adjust as necessary.) If this doesn't work, as Norm suggested get the process's output and error streams, and print them to see if they tell you why it refuses to run. The methods to use are getInputStream() and getErrorStream(), respectively. See Process (Java Platform SE 7 ). (Note: The getInputStream() method "returns the input stream connected to the normal output of the subprocess. The stream obtains data piped from the standard output of the process represented by this Process object." In other words, the Process object's InputStream is the actual process's output.)
    AlexGraal likes this.

  12. #12
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Lets see, is this what you wanted?

    Java Code:
    import java.util.*;
    import java.io.*;
    class test {
      public static void main ( String[] args ) {
        
        Runtime runTime = Runtime.getRuntime();
        try {
          Process process = runTime.exec("C:\\Octave\\3.2.4_gcc-4.4.0\\bin\\octave-3.2.4.exe");
          InputStream err = process.getErrorStream();
    
          InputStreamReader isr = new InputStreamReader(err);
          BufferedReader br = new BufferedReader(isr);
          
          String line;
          while ((line = br.readLine()) != null) {
            System.out.println(line);
          }
        } catch ( IOException e ) {
          e.printStackTrace();
        }
      }
    }
    In this case, the program simply runs infinitely. Nothing seems to print - it just keeps running and running.

    I'm appalled at how hard it seems to do something as simple as opening another program...though it is, in part, due to my complete lack of experience with the matter, it is still seemingly very convoluted....

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Running Programs through Java

    I never had much trouble with it myself, just follow what is said here and everything works out splendidly:

    When Runtime.exec() won't | JavaWorld

    What I basically did was use two threads to consume both the out and err lines of the program while the main thread was waiting for the forked process to finish. Its one of those things which becomes easy if you've done it once.

    Quote Originally Posted by AlexGraal View Post
    though it is, in part, due to my complete lack of experience with the matter, it is still seemingly very convoluted....
    Low level stuff such as forking processes always is. If you want it to be clean and idiot-proof (pardon the terminology) you have to look for a higher level framework to hide the low level bits of bops from you.
    Last edited by gimbal2; 03-31-2014 at 09:25 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    If nothing prints from the err stream, try the input stream and see if there is anything there.

    To see what the program prints on a console, open a command prompt window and execute the program there. Does it print anything on the console?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Running Programs through Java

    Ahh... I was wrongly assuming that running octave will launch a GUI. Octave (before v3.8) has a command-line interface only.

    The code in post #12 will expectedly "hang" because it is continuously waiting for output in the error stream, but will not display anything because there isn't anything in the error stream to display on the first place. As Norm suggested, if you replace getErrorStream() with getInputStream(), it will show
    Java Code:
    GNU Octave, version 3.2.4
    Copyright (C) 2009 John W. Eaton and others.
    ...
    For information about changes from previous versions, type `news'.
    but will still "hang" there because at this point octave is waiting for user input.

    I think what you'd want is for the Java program to execute octave on a new command prompt window to allow further user input after launching. This is where the JavaWorld article that gimbal2 helpfully referenced comes into play. The command to open a new command prompt window is "start". You can try this out on the Windows command prompt by typing "start octave.exe" (not sure what's the difference between octave.exe and octave-3.2.4.exe, so I'm using them interchangeably...)

    As stated in the article, "Runtime.exec() is not a command line." If you execute a Windows command like "start" (as opposed to an executable like notepad.exe) directly using Runtime.exec(), you'll get
    Java Code:
    java.io.IOException: Cannot run program "start": CreateProcess error=2, The system cannot find the file specified
    Instead you'll need to precede it with "cmd.exe /c", e.g.,
    Java Code:
    runTime.exec("cmd.exe /c start C:\\Octave\\3.2.4_gcc-4.4.0\\bin\\octave.exe")
    gimbal2, Norm and AlexGraal like this.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    octave is waiting for user input
    To pass data to a program, use the process's output stream and write the program's required input to that.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Wow. Thank you. This solves all of my problems. This'll also work for all of the other programs that I wanted to open. Amazing.

    One last question before I consider this thread over:

    It doesn't matter much what the directory/folder with the executable file contains. Windows will just go through all the directories listed in PATH, and check if the filename you typed matches any of the executable files in the directories. It won't try to execute non-executable files such as .dll files as Windows determines file type through the filename extension.
    You said this earlier, Jashburn.

    From a conversation with one of my friends, I seem to remember hearing something about how if I try to open a file or run a file and it looks for it in the path, the first instance of that file or program in the path will be the one taken. So by adding more stuff to your path, or folders with huge amounts of commonly named files/programs, you can create issues for yourself later on. For example, if a hacker could place a .exe called shutdown in the first location in your path which would send the hacker all of your personal information, and your computer had to look for and execute a shutdown.exe which happened to be in the second location in your path, every time you tried to "shut down" your computer, it would actually send the hacker this information.

    Is that how it actually works? Are there reasons NOT to add more stuff to your path? Does it slow the computer's search time down or something along those lines?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Running Programs through Java

    if a hacker could place a .exe
    Then he could do anything. The PATH wouldn't be part of the problem.

    A longer PATH would mean it would take longer for the OS to say a program was not found.
    If PATH entries are in the wrong order, it is possible the wrong program could be chosen because it was found first.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Running Programs through Java

    Indeed. If a hacker has access to your system you have bigger problems than what is on the search path :) IE: it is actually possible for hackers to break into your computer!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Running Programs through Java

    Alright, one more problem here, demonstrating my lack of knowledge of paths.

    So, I've placed a folder on my desktop with my program. Then, I took the path of the folder, C:\Users\Alex\Desktop\FolderName, and added it to my local path variable...

    PATH = blah;blah;blah;C:\Users\Alex\Desktop\FolderName

    Now why can't I simply type "java openClass" in my cmd without having to cd to the actual location?

    When I try to do that, I get the error message, "Error: Could not find or load main class openClass." What's going on here?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 09-21-2012, 01:47 PM
  2. Running Java programs in windows 7
    By jonytek in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 04:45 AM
  3. Replies: 6
    Last Post: 04-20-2009, 04:34 AM
  4. Running 3 Java programs in sequence.
    By loktamu in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 06:50 PM
  5. Problem in running java programs
    By aravind in forum New To Java
    Replies: 0
    Last Post: 07-14-2008, 11:19 AM

Posting Permissions

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