Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 46
  1. #21
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i just ran the program w/ cat.exe and no cmd promt show up.
    I see where the problem is and will be playing around w/ it. prob get an i/o stream.

    "start" is not what you wnt in this case cause it starts a new process from that.

    Is your c++ app win32 or console?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    Well... I got my command and it works fine, but I dont get the proper exit value...

    Here's the command: cmd /c start /wait ConsoleApplications.exe

    I think that I'm not getting the proper exit value because I'm getting the one returned by the command, not the app. Anyone know of a way around this?

    EDIT: Missed AB's post again... It's a console app.
    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!

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

    Default

    This is certainly cheesy compared to monitoring a process, but cmd /c start app.exe does get the app running.

    One way to find out when the app completed is to create an empty file and monitor it Have app update the file when it completes. I believe you can add an update listener in the Java program, so you don't even have to poll the file. You can get the return code from the file.

    Not the greatest, but it would work...

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

    Default

    Good trick. Perhaps you can chain "exit" to your application and use the application return code in the exit command.

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

    Default

    chain exit? can you give me an example?

    And it's not a trick... came up just typing start /? in the command prompt, though it took a bit of reading.
    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. #26
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    cmd /? > c:\info.txt && notepad c:\info.txt
    AB redirects the output of help to a file and then brings up notepad to view the file. "&&" seems to be the key. You would want to find a way to capture the value returned by your app. Actually, the easy thing to do would be to start a .bat file containing a call to your program and the exit command. You could put the result of of the call into a variable and pass it to exit.

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

    Default

    I don't think command line exits can return values, and I can't find anything on batch files that will tell me how to do it... Any help?

    Hoping for the best
    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!

  8. #28
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Firstly, it makes no difference what language you wrote your native app in, C++, C, J# or whatever. All processes return an integer exit code.

    Secondly, why Lord why are you doing this? Don't specifically design applications so that you have to run a native Process from Java. If you want to write native code and call it from Java use the JNI.

    If you want to interact with a native process in Java, use the streams provided by Process, don't try messing around with random Windows consoles.

    If you do want to try messing around with random Windows consoles, do all your work from the console to start with. Don't try and start cmd consoles from GUI applications.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    This is a great reference site for Windows commands.

    "exit" can accept an error level numeric value. You should be able to retrieve that when the process ends.

  10. #30
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I think you actually meant Windows Commands, which is a list of command-line commands. To return status codes, you should use the system call exit() from within your code. In C++, whatever you return from the main method gets returned as the result of such a call.
    Last edited by OrangeDog; 05-29-2009 at 04:55 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    Why am I doing this...

    A: To learn something... I've never tried anything with Processes, and the JNI is just confusing to me...

    B: I've had a few ideas for programs related to this, but I can't create those until I figure out how this works.
    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. #32
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    No, I actually meant that Boyo could make an inference from the specific to the general. Sending him to the parent page would not have helped him; sending him to the specific page helped him twice.

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

    Default

    I normally avoid stuff like this like the plague, but this thread is interesting...

    I think you are close.

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

    Default

    Close, yes, but one problem...

    Can't figure out how to retrieve the exit code of the program... If I can do that, I'll be able to do a whole lot more...
    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. #35
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    To get the exit code just do what I recommended earlier...

    Java Code:
    Process proc = Runtime.getRuntime().exec("somefile.exe");
    proc.waitFor();
    int code = proc.exitCode();

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

    Default

    @Richard Jeez... read the posts... We're talking about batch files retrieving exit codes here...
    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!

  17. #37
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    @Steve: What I meant was your link doesn't work at all. It goes to a German site that provides digital signatures for business use.

    @SB: Adding a batch file into this mess is completely the wrong idea. You can get the exit code from a program directly via the Process API.

    Know which tools are appropriate. If you want to work with native APIs or system design, don't use Java - it's not designed for it. If you want to do some kind of scripting, use a proper scripting language (Perl, Python, Ruby). Yes, I can write a web browser in ML, or a text adventure in Microsoft Excel, but I'd probably get sectioned for doing so with serious intent.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    Hmm... it's a mess, but hey, I'm close now...
    problem for me is that I cant seem to run the C++ app directly, but I need to go through the command line. Would be nothing wrong with that, but the exit code is not working properly...

    I was originally just trying to get it to run... but then I had a few other ideas and can't get it to work well... you would think someone would have a decent tutorial so I could convert my C++ code to JNI, but I can't find one.
    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!

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

    Default

    am i too late? been having net troubles...

    Java Code:
          Process p = Runtime.getRuntime().exec("go.bat");
          p.waitFor();
          System.out.println(p.exitValue());

    bat file
    Java Code:
    @echo off
    start /wait myapp.exe
    exit %errorlevel%
    edit:
    oh it was:
    cmd /? > c:\info.txt & notepad c:\info.txt

    only 1 & not 2 &&
    Last edited by angryboy; 05-29-2009 at 06:14 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    You were a bit late AB. Still can't get the proper return code from my C++ app... it's always 0
    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!

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Executing a jar file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 02-14-2014, 09:02 AM
  2. Replies: 2
    Last Post: 03-09-2009, 02: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, 03:38 AM
  4. How to find file created date.....
    By roshithmca in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 09:48 AM
  5. Executing Ant code pragramatically
    By MikeO in forum Advanced Java
    Replies: 0
    Last Post: 07-24-2007, 09: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
  •