• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Greenhorn
Posts: 9
  • 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();
}
}
}
 
lowercase baba
Posts: 12738
51
Chrome Java Linux
  • 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.
 
author
Posts: 23830
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
 
Ranch Hand
Posts: 174
  • 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...
 
Sheriff
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • 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
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • 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
  • 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
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • 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
  • 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
  • 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
 
Sheriff
Posts: 24381
55
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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.

 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!