aspose file tools*
The moose likes Beginning Java and the fly likes Space within a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Space within a string?" Watch "Space within a string?" New topic
Author

Space within a string?

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Hi I am passing a path from a string which is "c:\\ my path \\here.java

but when i run this it will take only till "c:\\my and says your path is not correct

i want to figure out the space between my and path so that i can have the full path .. please help me out


No Kaustubh No Fun, Know Kaustubh Know Fun..
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

I tried with "c:\\my"+" "+"path\\rest

but it is not working too
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Passing from what to what? I think somewhere the operating system is involved (Runtime.exec perhaps) but I'm not sure until you TellTheDetails.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

I am passing that string to command prompt in java from main function

this is what i am passing


after executing this it's throw error d:\program directory couldn't found
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try:

And read Runtime.exec().


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

the problem is (i believe) when the OS gets your string, it breaks your string up into tokens based on the space. You need to tell it that the entire string is the path, and to not break it up. To do that, you need to surround it in quotes. To put quote characters inside a string in java, you need to escape them - which is what Wouter posted above.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

fred rosenberger wrote:the problem is (i believe) when the OS gets your string, it breaks your string up into tokens based on the space. You need to tell it that the entire string is the path, and to not break it up. To do that, you need to surround it in quotes. To put quote characters inside a string in java, you need to escape them - which is what Wouter posted above.


The damage is done before the command ever gets to the OS. Runtime has overloaded methods public Process exec(String command, String[] envp, File dir), public Process exec(String command, String[] envp) and public Process exec(String command). On the surface, these would seem to be ideal for just creating a simple command line without having to go to the bother of constructing an array BUT do not be deceived. The second two methods delegate to the first (with appropriate values of unused parameters) and Looking at the source code for Process exec(String command, String[] envp, File dir) one sees :-


Ouch! All this does is to split the command String at white spaces and create an array of the fragments which is then passed to the overloaded exec() method that takes the array as argument. What is worse, multiple spaces are in effect reduced to single spaces and my experiments indicates that Linux and Windows behave differently.

My advice - don't even think about using the single String version of exec() (or ProcessBuilder) because it will at some point jump up and bite you.

What the OP needs to do is something along the lines of -


Note the absence of the 'start'; this is rarely needed.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

thanks guys
 
 
subject: Space within a string?