Solved

Escape sequence in java

Posted on 2001-09-04
8
1,183 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
kotan earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now