Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Space within a string?

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with "c:\\my"+" "+"path\\rest

but it is not working too
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing from what to what? I think somewhere the operating system is involved (Runtime.exec perhaps) but I'm not sure until you TellTheDetails.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

And read Runtime.exec().
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic