Solved

Escape sequence in java

Posted on 2001-09-04
8
1,187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
by zero exception 10 70
Why is enum singleton a better approach than static factory 3 52
import as existing maven project 3 42
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

696 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