aspose file tools*
The moose likes Java in General and the fly likes the exec command is not able to deal with space some time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "the exec command is not able to deal with space some time" Watch "the exec command is not able to deal with space some time" New topic
Author

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

sam salo
Greenhorn

Joined: Jun 08, 2010
Posts: 9
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
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...


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Jun 08, 2010
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Jun 08, 2010
Posts: 9
Yes The orginal code is working after I changes this line from



to



2 Solutions is hand
sam salo
Greenhorn

Joined: Jun 08, 2010
Posts: 9
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
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Maybe you should stick to playing hockey for another few years?



No, just joking, carry on with the Java!
sam salo
Greenhorn

Joined: Jun 08, 2010
Posts: 9
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the exec command is not able to deal with space some time
 
Similar Threads
Java Runtime command syntax help
How to send comands to anoyther program
Runtime.getRuntime().exec() not printing output
try to run commands in command prompt by using java
Executing a *.bat file