• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Space within a string?

 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with "c:\\my"+" "+"path\\rest

but it is not working too
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try:

And read Runtime.exec().
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic