Solved

Escape sequence in java

Posted on 2001-09-04
8
1,181 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

16 Experts available now in Live!

Get 1:1 Help Now