Results 1 to 20 of 46
- 05-28-2009, 03:02 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-28-2009, 03:43 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
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(); } } }
- 05-28-2009, 04:00 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-28-2009, 04:02 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Are you running on XP or Vista?
- 05-28-2009, 05:07 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-28-2009, 05:22 AM #6
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)
- 05-28-2009, 05:46 AM #7
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.
- 05-28-2009, 05:48 AM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 knowIf 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!
- 05-28-2009, 07:00 AM #9
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 05-28-2009, 07:58 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-28-2009, 08:32 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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:
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!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"); } }Last edited by Singing Boyo; 05-28-2009 at 08: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!
- 05-28-2009, 01:28 PM #12
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
You need to do this...
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.Java Code:Process p = Runtime.getRuntime().exec("cmd /c start ConsoleApplications.exe"); p.waitFor(); processExitValue = p.exitValue();
- 05-28-2009, 04:48 PM #13
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.
- 05-29-2009, 01:10 AM #14
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
@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 BoyoIf 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!
- 05-29-2009, 01:56 AM #15
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.
- 05-29-2009, 02:04 AM #16
cmd /? > c:\info.txt && notepad c:\info.txt
its loooong...USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-29-2009, 02:06 AM #17
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-29-2009, 02:11 AM #18
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?
- 05-29-2009, 02:32 AM #19
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-29-2009, 02:43 AM #20
Similar Threads
-
error while running jar file created from netbeans
By ecstatic in forum NetBeansReplies: 2Last Post: 03-09-2009, 02:36 AM -
How to stop thread from being jumping off the code without executing it.....
By chiragkini in forum Threads and SynchronizationReplies: 6Last Post: 01-22-2009, 03:38 AM -
How to find file created date.....
By roshithmca in forum Advanced JavaReplies: 1Last Post: 02-18-2008, 09:48 AM -
Executing Ant code pragramatically
By MikeO in forum Advanced JavaReplies: 0Last Post: 07-24-2007, 09:34 PM -
Executing a jar file
By peiceonly in forum New To JavaReplies: 2Last Post: 04-06-2007, 02:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks