Solved

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

Posted on 2006-07-21
37
2,853 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
  • 13
  • 12
  • 10
  • +1
37 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
ttry specifying the fll path to a.exe
0
 
LVL 3

Author Comment

by:a122178
Comment Utility
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
Comment Utility
0
 
LVL 3

Author Comment

by:a122178
Comment Utility
mistake

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

0
 
LVL 3

Author Comment

by:a122178
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
noo i meant where the exe is writing its output
0
 
LVL 86

Expert Comment

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

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
use:

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
cool. yeah.. I read the article. I am going to do the experiment now. Let's see.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Expert Comment

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

Author Comment

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

Author Comment

by:a122178
Comment Utility
CEHJ:

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

Author Comment

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

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
You are, i hope, now reading stderr and stdout?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
Can you please post here the command that works for you on the command line?
0
 
LVL 3

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
OK - should be alright. Make sure you're reading both streams
0
 
LVL 3

Author Comment

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

Accepted Solution

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

Assisted Solution

by:objects
objects earned 250 total points
Comment Utility
> 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
Comment Utility
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
Comment Utility
Try

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now