Solved

IOException CreateProcess error = 5 after Runtime.getRuntime().exec(command )

Posted on 2006-07-21
37
2,936 Views
Last Modified: 2008-01-09
Hi all,

I have a problem with Runtime.getRuntime().exec(). I have made one execute file, example a.exe, that will return a String that I need for on the dos command. I need to read Hello World and put it in my java class.

>> a .exe hello
>> Hello World

Code:

public String getWindowExecResult(String command){
        try{
           
            Process process = Runtime.getRuntime().exec( command );
            StreamReader reader = new StreamReader(process.getInputStream());
           
            reader.start();
            process.waitFor();
            reader.join();
           
            return reader.getResult();
           
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

I want to run this on my application. I use Runtime.getRuntime().exec("a.exe hello").

But I got an error java.IOException CreateProcess error = 5

I tried Runtime.getRuntime().exec("cmd \c a.exe hello"). No error message, but cannot read the result string.

What am I doing wrong?

Thanks,
0
Comment
Question by:a122178
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
  • 10
  • +1
37 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17158406
ttry specifying the fll path to a.exe
0
 
LVL 3

Author Comment

by:a122178
ID: 17158457
Thanks for comment. But I tried the whole path already.

...exec("C:\\FOLDER"\a.exe hello);

Still not work.

It seems
0
 
LVL 5

Expert Comment

by:tbboyett
ID: 17158459
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 3

Author Comment

by:a122178
ID: 17158460
mistake

...exec( " \"C:\\FOLDER\"\\a.exe hello" );

0
 
LVL 3

Author Comment

by:a122178
ID: 17158469
It seems this is correct.


public String getWindowExecResult(){
        try{
 
            Process process = Runtime.getRuntime().exec( "cmd /C " + scriptPath + "winsetes.exe computername");
            StreamReader reader = new StreamReader(process.getInputStream());
           
            reader.start();
            process.waitFor();
            reader.join();
           
            return reader.getResult();

        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }


If I do it on the command line:

C:\Projects\DEV\Remote_Reimaging_Tools\tools\bin>winsetes fpshare
Result:2,0,File and Print services are installed.


But I print out the String reader.getResult() is empty
0
 
LVL 92

Expert Comment

by:objects
ID: 17158478
yes you need cmd there to get a shell

are you sure the exe is wrting to stdout? check if anything is being printed to stderr
0
 
LVL 3

Author Comment

by:a122178
ID: 17158507
yeah.. I put system.out.println and check the console.

Nothing is there. Do you think it prints out the output of cmd instead of the printout inside cmd?

public String getWindowExecResult(){
        try{
 
            Process process = Runtime.getRuntime().exec( "cmd /C " + scriptPath + "winsetes.exe computername");
            StreamReader reader = new StreamReader(process.getInputStream());
            System.out.println("This is the result: " + reader.getResult() + " End");
            reader.start();
            process.waitFor();
            reader.join();
           
            return reader.getResult();

        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 17158523
noo i meant where the exe is writing its output
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17159154
You need two readers when executing a process; one to read stderr and one to read stdout
0
 
LVL 92

Expert Comment

by:objects
ID: 17159158
> You need two readers when executing a process; one to read stderr and one to read stdout

no you don't. you only need to read from stderr if there is output that you are interested in going there. Which has already been mentioned anyway :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17159159
>>no you don't. you only need to read from stderr if there is output that you are interested in going there

You need to read it whether or not you're interested in it
0
 
LVL 92

Expert Comment

by:objects
ID: 17159166
incorrect (again), you only need to read it if there is lots of it.
Which is defintiely not the case here so not at all relevant.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17159172
0
 
LVL 92

Expert Comment

by:objects
ID: 17159178
As I had already stated "check if anything is being printed to stderr"
Not sure why CEHJ feels the need to repeat it :)
0
 
LVL 3

Author Comment

by:a122178
ID: 17171625
Thanks for the comments. Sorry for late reply.
I check the print out from System.out.println. But I could not see anything.
And First of all,

should I use:

1) Runtime.getRuntime().exec( "cmd /C " + scriptPath + "winsetes.exe computername");

or

2) Runtime.getRuntime().exec( scriptPath + "winsetes.exe computername");

Result:
1) No error, but cannot get the output string from CMD.
2) Error, java.IOException CreateProcess error = 5. I check online. It means "Access denied" of the exe. But the permission is fine.

Thanks for help.

0
 
LVL 92

Expert Comment

by:objects
ID: 17171666
use:

1) Runtime.getRuntime().exec( "cmd /C " + scriptPath + "winsetes.exe computername");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17171672
1 looks OK, although i'm not certain as i don't know the exe. Did you read the article to which i posted a link?
0
 
LVL 3

Author Comment

by:a122178
ID: 17171728
cool. yeah.. I read the article. I am going to do the experiment now. Let's see.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17171741
What, incidentally, is the value of 'scriptPath'?
0
 
LVL 3

Author Comment

by:a122178
ID: 17171845
"C:\Program files\Altiris\eXpress\Deployment Server"\tools\winsetes.exe
0
 
LVL 3

Author Comment

by:a122178
ID: 17171850
CEHJ:

Thanks for the article. But it does not work. Cannot see any output.
0
 
LVL 3

Author Comment

by:a122178
ID: 17171861
Sorry the scriptPath: "C:\\Program files\\Altiris\\eXpress\\Deployment Server"\\tools\\
0
 
LVL 92

Expert Comment

by:objects
ID: 17171883
> Thanks for the article. But it does not work. Cannot see any output.

No that articles dicusses different issues to yours

> Sorry the scriptPath: "C:\\Program files\\Altiris\\eXpress\\Deployment Server"\\tools\\

try:

Runtime.getRuntime().exec( "cmd /C \"C:\\Program files\\Altiris\\eXpress\\Deployment Server\\tools\\winsetes.exe\" computername");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17171885
You are, i hope, now reading stderr and stdout?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17171892
>>No that articles dicusses different issues to yours

How do you know? If you know the issues, then please post the solution right away
0
 
LVL 92

Expert Comment

by:objects
ID: 17171943
also as a test move these to a directory without spaces.

also try:

Runtime.getRuntime().exec( new String[] { "cmd",  "/C",  "C:\\Program files\\Altiris\\eXpress\\Deployment Server\\tools\\winsetes.exe", "computername"});
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17171999
Can you please post here the command that works for you on the command line?
0
 
LVL 3

Author Comment

by:a122178
ID: 17172195
Runtime.getRuntime().exec( "cmd /c " + "C:\\Program files\\Altiris\\eXpress\\Deployment Server\\tools\\winsetes.exe" + " computername ");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17173883
Well, if that's an answer to my last question, i was asking you to post just the command that works on the command line (nothing to do with Java)
0
 
LVL 3

Author Comment

by:a122178
ID: 17177772
Sorry for the misunderstanding. This is the command:

>> C:\Program files\Altiris\eXpress\Deployment Server\tools\winsetes.exe  computername
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17177992
OK - should be alright. Make sure you're reading both streams
0
 
LVL 3

Author Comment

by:a122178
ID: 17180348
I make sure to do both streams. Still a problem.
um... :(
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 17180664
Does it hang or just terminate with no output?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 17182215
> I make sure to do both streams.

as i've mentioned earlier the problem is *not* with your streams.
Did u try the suggestions I posted above.


0
 
LVL 3

Author Comment

by:a122178
ID: 17365281
Thanks. I have tried to move it to the folder without space. Still not work. There is no hang. It just does nothing.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17365383
Try

Runtime.getRuntime().exec(new String[] { "cmd", "/c", "C:\\Program files\\Altiris\\eXpress\\Deployment Server\\tools\\winsetes.exe", "computername" } );
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question