File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the exec command is not able to deal with space some time

 
sam salo
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I make this program work ? thanks I can not do java -version and guessing the space is producing an issue

import java.io.*;
public class TestExec {
public static void main(String[] args) {
String[] command = new String[4];
command[0] = "cmd";
command[1] = "/C";
command[2] = "java";
command[3] = "-version";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, welcome to the ranch. A couple of tips:

Please use code tags when posting. It makes your source MUCH easier to read.

Please tell us what the 'issue' is, exactly. those error messages mean something.

I don't know if you can pass an array into the exec command, but if so, I think the answer would be something like:

command[0] = "cmd ";
command[1] = "/C ";
command[2] = "java ";
command[3] = "-version ";

in other words, try put spaces in your strings as needed. I don't know if it will work, but it's worth a shot.
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No. Adding spaces to the elements of the array isn't necessary.

You should, however, print out the error, and tell us what it is -- particularly the error messages going to stderr too.

Henry
 
Peter Taucher
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see no problem, your 'command' array seems to work for me (both with Runtime#exec or ProcessBuilder):



Of course, 'Test 3' doesn't work with either one. But as long as you separate each parameter, there shouldn't be issues with blanks...
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -version prints to the error stream, not the output stream. If I use the original code and replace getInputStream() with getErrorStream() I see the results I expected.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Taucher wrote:

I think that the following will be just a bit more efficient, as it doesn't need to context switch each 100ms:
Both calls will block until the thread is done; exactly what you want.
 
sam salo
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick response
Yes I will put future code in code tags.
The issue that I having, there is no console output or print out from the command line when I run the program that I provided.
Yes there is an output when I execute this portion of the code.


but once I add the -version there is no output, not sure why, is locking or what ?
The provided solution by Peter Taucher seems to be working fine, thank you, but I thought it is an overkill for what I'm trying to do and that is writting simple junit to check for the java -verison.
At least now I have a solution that I can use.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam salo wrote:but once I add the -version there is no output, not sure why, is locking or what ?

Rob Prime wrote:java -version prints to the error stream, not the output stream. If I use the original code and replace getInputStream() with getErrorStream() I see the results I expected.

I just hate it when people blatantly ignore my posts. It makes me want to ignore them too. I doubt you want that.
 
sam salo
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes The orginal code is working after I changes this line from



to



2 Solutions is hand
 
sam salo
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime You are great man, I was not ignoring your post, I appreciate your help, just things were moving 2 fast for me, or I feel that there was a delay in the appearance of the post
 
Paul Clapham
Sheriff
Pie
Posts: 20160
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should stick to playing hockey for another few years?



No, just joking, carry on with the Java!
 
sam salo
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Maybe you should stick to playing hockey for another few years?



No, just joking, carry on with the Java!


You are a piece of ART, why waste your time here, you should be a comedian.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic