Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3014
  • Last Modified:

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

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
a122178
Asked:
a122178
  • 13
  • 12
  • 10
  • +1
2 Solutions
 
objectsCommented:
ttry specifying the fll path to a.exe
0
 
a122178Author Commented:
Thanks for comment. But I tried the whole path already.

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

Still not work.

It seems
0
 
tbboyettCommented:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
a122178Author Commented:
mistake

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

0
 
a122178Author Commented:
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
 
objectsCommented:
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
 
a122178Author Commented:
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
 
objectsCommented:
noo i meant where the exe is writing its output
0
 
CEHJCommented:
You need two readers when executing a process; one to read stderr and one to read stdout
0
 
objectsCommented:
> 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
 
CEHJCommented:
>>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
 
objectsCommented:
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
 
CEHJCommented:
0
 
objectsCommented:
As I had already stated "check if anything is being printed to stderr"
Not sure why CEHJ feels the need to repeat it :)
0
 
a122178Author Commented:
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
 
objectsCommented:
use:

1) Runtime.getRuntime().exec( "cmd /C " + scriptPath + "winsetes.exe computername");
0
 
CEHJCommented:
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
 
a122178Author Commented:
cool. yeah.. I read the article. I am going to do the experiment now. Let's see.
0
 
CEHJCommented:
What, incidentally, is the value of 'scriptPath'?
0
 
a122178Author Commented:
"C:\Program files\Altiris\eXpress\Deployment Server"\tools\winsetes.exe
0
 
a122178Author Commented:
CEHJ:

Thanks for the article. But it does not work. Cannot see any output.
0
 
a122178Author Commented:
Sorry the scriptPath: "C:\\Program files\\Altiris\\eXpress\\Deployment Server"\\tools\\
0
 
objectsCommented:
> 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
 
CEHJCommented:
You are, i hope, now reading stderr and stdout?
0
 
CEHJCommented:
>>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
 
objectsCommented:
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
 
CEHJCommented:
Can you please post here the command that works for you on the command line?
0
 
a122178Author Commented:
Runtime.getRuntime().exec( "cmd /c " + "C:\\Program files\\Altiris\\eXpress\\Deployment Server\\tools\\winsetes.exe" + " computername ");
0
 
CEHJCommented:
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
 
a122178Author Commented:
Sorry for the misunderstanding. This is the command:

>> C:\Program files\Altiris\eXpress\Deployment Server\tools\winsetes.exe  computername
0
 
CEHJCommented:
OK - should be alright. Make sure you're reading both streams
0
 
a122178Author Commented:
I make sure to do both streams. Still a problem.
um... :(
0
 
CEHJCommented:
Does it hang or just terminate with no output?
0
 
objectsCommented:
> 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
 
a122178Author Commented:
Thanks. I have tried to move it to the folder without space. Still not work. There is no hang. It just does nothing.
0
 
CEHJCommented:
Try

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 13
  • 12
  • 10
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now