Page 1 of 3 123 LastLast
Results 1 to 20 of 46
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Problem executing .exe file created with C++ code

    Hi there all.

    I have recently started trying to gain some experience with C++ code, but I also wanted to expand my Java experience, so I tried created a Java program that would execute the C++ .exe file for the C++ program. The C++ program started fine, but the console that shows up by just double-clicking the .exe file from within My Computer (I'm running Windows) does not appear, and I then have to go through task manager to end the C++ program, and the Java then ends (using the big red terminate button in Eclipse does not end the C++ program)

    I'm wondering if anyone knows a better way to execute the program than what I am using... (Runtime.getRuntime().exec(command)) Maybe I need to change my C++ code, but it works fine with the regular double-click...

    Note: The .exe file does contain a line stating that the program does not run through command prompt (double-clicking it in Eclipse showed text... sort-of). I'm not too familiar with Runtime.exec(command), but I believe this may have something to do with it.

    Here's my code... the println happens but the C++ console does not appear.

    Java Code:
    import java.io.IOException;
    
    public class CPPExec {
    
    	public static void main(String[] args){
    		try {
    			Runtime.getRuntime().exec("ConsoleApplications.exe");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		System.out.println("Execution complete");
    	}
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    I don't fully understand your question but I can tell you one thing you are doing wrong.. try using the following code

    Java Code:
    public class CPPExec
    {
      public static void main(String[] args)
      {
          try
          {
              Process proc = Runtime.getRuntime().exec("ConsoleApplications.exe");
              proc.waitFor();
              System.out.println("Execution complete");
        }catch(Exception e)
        {
             e.printStackTrace();
        }
      }
    }

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'll try to explain my question more fully...

    The basic question is... how do you run a C++ program from Java? I managed to execute the .exe file, but the C++ program did not work the same way as it does when run without using the Java code. (No console appeared)

    @Richard Thanks for the suggestion. I just can't test it until I get the C++ program running properly.

    Note: The line in the .exe file says that it cannot be run in DOS mode, not that it cannot be run through command prompt.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Are you running on XP or Vista?

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'm running on XP
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    it should matter if its c++/c or whatever.
    have you try making it work w/ notepad first?

    try calling it w/:

    "cmd /c appName.exe"

    "cmd /c start appName.exe"
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I'm no expert in this area, but I think what AB is getting at is that you won't get a command window without invoking "command" or "cmd" to run the application. Windows does that automatically because it recognizes that it is not a "Windows" program.

    I believe the difference in the two commands he lists is that one will wait for the program to complete, while the other will start it in a separate process and go on.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Thanks angryboy, the cmd /c start worked... now to figure out how to determine when that program exits, and how to run it if I'm using a jar file...

    waitFor() just waited for the console to show up...

    EDIT: Didn't see steve's post... the cmd /c did not do anything, so cmd /c start is my only option as far as I know
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Double clicking in windows executes javaw, not java. javaw intentionally suppresses console windows to make java apps look like native windows apps.

    If you want a console, you can bring one up, and then launch your java app from within it using the 'java' command. You can also create a shortcut that executes 'java' rather than 'javaw', and that will bring up a terminal window too.

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    console came up fine with cmd /c start. Now I just need to know when the program exits (get it's return value -- for anyone who doesn't know, C++ returns ints from main methods) and how to run it in a jar file (would I need to have a folder containing all the C++ code as well?)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Took another look at the Process.waitFor() method and discovered it returns an int... the problem is that it is returned early from my C++ program (returned before the program has exited) I believe this is most likely a problem with the code I am using (Below). Any hints?

    My Code:
    Java Code:
    import java.io.IOException;
    
    public class CPPExec {
    
    	public static void main(String[] args){
    		int processExitVal = 0;
    		try {
    			Process p = Runtime.getRuntime().exec("cmd /c  start ConsoleApplications.exe");
    			processExitVal = p.waitFor();
    		} catch (IOException e) {
    			System.out.println("IOException");
    			e.printStackTrace();
    		} catch (InterruptedException e) {
    			System.out.println("InterruptedException");
    			e.printStackTrace();
    		}
    		System.out.println(processExitVal);
    		System.out.println("Execution complete");
    	}
    }
    The System.out.println calls print before my C++ program has exited, and processExitVal is always 0 (even when I make the C++ code return a different value). All and any help is welcome!
    Last edited by Singing Boyo; 05-28-2009 at 09:46 AM. Reason: Fixed a few typos
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    You need to do this...

    Java Code:
         Process p = Runtime.getRuntime().exec("cmd /c  start ConsoleApplications.exe");
         p.waitFor();
         processExitValue = p.exitValue();
    If you'r C++ app is printing stuff out to the command line then you need to grab the InputStream via p.getInputStream(); to read the data from the command line.

  13. #13
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Back to what I was saying before... I *believe* cmd start app.exe starts the app in a separate process, so control immediately returns to cmd, which then terminates. Therefore, everything is doing what it is supposed to. Try removing the "start" and see what happens. I *believe* this will cause cmd to wait until app completes.

    @Toadaly- You're right about what you said about java and javaw. I was talking about cmd...

    As far as running from a JAR, it doesn't make any difference, as long as app.exe is not in the JAR.

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @Richard I want a separate command line for the C++ program, and I have that, but my code does not wait for the C++ program to exit, despite using waitFor().

    @Steve cmd /c app.exe does not work... no command line window shows up.

    Tried to find C++ execution commands for the command prompt, but couldn't find anything. If anyone knows some, post them and I'll give them a try.

    Thanks for all the help so far,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Actually, that makes sense. Without start, you are not starting a new process, and the current process does not have a window.

    With start, the process you are monitoring is the one that starts another, so it ends almost immediately.

    Try using "start" directly and monitoring that process. That might work.

  16. #16
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    cmd /? > c:\info.txt && notepad c:\info.txt

    its loooong...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  17. #17
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Still no luck with one that creates a command prompt window and exits at the right time, though just using exec("MyApp.exe) returns the correct exit code as far as I can tell. I may just need to make it so that my C++ code starts a new console... or figure out how to create a new command prompt console with Java and make it handle the C++ program.

    EDIT: Didn't see AB's post... What???
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  18. #18
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    AB told you how to put the cmd help into a file and then open the file with notepad.

    Did you try "start" by itself?

  19. #19
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    if you meant cmd start myApp.exe or just plain start myApp.exe neither worked... the first one just started a command prompt that never showed up, the second one gave me an error...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  20. #20
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Yes, that makes sense, now that you told me the result. "start" has to be run from the command line or in a batch file...

    I think AB's original idea was the best, but Windows apparently doesn't like it. I think you are running into a limitation of Windows.

Page 1 of 3 123 LastLast

Similar Threads

  1. Executing a jar file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 02-14-2014, 10:02 AM
  2. Replies: 2
    Last Post: 03-09-2009, 03:36 AM
  3. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 04:38 AM
  4. How to find file created date.....
    By roshithmca in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 10:48 AM
  5. Executing Ant code pragramatically
    By MikeO in forum Advanced Java
    Replies: 0
    Last Post: 07-24-2007, 10:34 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
  •