Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Escape sequence in java

Posted on 2001-09-04
8
Medium Priority
?
1,193 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

715 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