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

x
?
Solved

Escape sequence in java

Posted on 2001-09-04
8
Medium Priority
?
1,197 Views
Last Modified: 2007-12-19
Hi,
  I have a problem in executing a command which takes parameters in double quotes. Seems like the following program is ignoring the double quotes, can any body help how this can be done in Java, I used to do these things in C using escape sequece (\" \").

String s = "";
s = new String ("/myfiles/command " + "-P");
Process p = Runtime.getRuntime.exec(s);

is getting executed on the command line as /myfiles/command -P instead of /myfiles/command "-P".

I tried "\"-P\"" and many combination like this unsucessfully.

thanks
Aravind
0
Comment
Question by:arvindhp
8 Comments
 
LVL 4

Expert Comment

by:sdussinger
ID: 6453947
It appears that the Java Runtime class parses the string you pass in and breaks on whitespace no matter what you do.

A solution to this would be:

String [] argVector = {"/myfiles/command", "-P"}
Process p = Runtime.exec (argVector);

This way, any arguments which you normally would have put into quotes will still be treated as a single argument, rather than multiple arguments split on whitespace.

HTH

--Steve
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6454168
It appears that the Java Runtime class parses the string you pass in and breaks on whitespace no matter what you do.

A solution to this would be:

String [] argVector = {"/myfiles/command", "-P"}
Process p = Runtime.exec (argVector);

This way, any arguments which you normally would have put into quotes will still be treated as a single argument, rather than multiple arguments split on whitespace.

HTH

--Steve
0
 
LVL 1

Expert Comment

by:eattrig
ID: 6454834
How were you using the \" character in conjunction with String?  Can you submit what you have tried?  I have tried instantiating String with "/myfiles/command \"-P\"" and outputing the String to the console and come back with /myfiles/command "-P", just like you wanted.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:sdussinger
ID: 6454857
eattrig:

I tried a whole bunch of different ways to do this, just like arvindhp did. I also wrote a little C program which simply counted its args and displayed the results.

Whenever I tried to handle a quoted string using Runtime.exec(String), it failed to create the arguments correctly. If the quoted arg does not contain spaces, then it works as you expect. However, if the quoted arg does contain spaces, the Runtime.exec (String) method breaks the quoted string at the spaces.

As near as I can tell, the issue here isn't in the String class. That works just fine. You can easily create a string with embedded quotes, no problem. The problem comes in when you try to pass that string to the Runtime.exec(String) method.


--Steve
0
 
LVL 6

Accepted Solution

by:
kotan earned 150 total points
ID: 6455417
Try this

String [] argVector = {"/myfiles/command", "\"-P\""}
Process p = Runtime.exec (argVector);
0
 

Expert Comment

by:hanumansetti
ID: 6456482
Try this,

String s = "";
s = new String ("/myfiles/command " + "\"-P\"");


If u give System.out.println(s);

Output:
=======

/myfiles/command "-P"

0
 
LVL 3

Expert Comment

by:daitt
ID: 6456787
I think it's "/myfiles/command \\\"-P\\\"" :)
Regards
Dai
0
 

Author Comment

by:arvindhp
ID: 6464996
Hi,
  this answer is OK, but when I compile this I am getting the following error, can any one help how to proceed.
--------------------------------------------
argVector.java:571: Can't make static reference to method java.lang.Process exec(java.lang.String) in class java.lang.Runtime. Process p = Runtime.exec(argVector);
----------------------------------------------------
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

886 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