0
   Desktop desktop = null;
   if (Desktop.isDesktopSupported()) 
   {
     desktop = Desktop.getDesktop();
   }

   desktop.open(new File("c:\\SRC\\shankar\\a.doc"));    

Here i don't want to specify the Drive C:.

I need my code like

desktop.open(new File("\\\SRC\\\shankar\\\a.doc"));

Is there is any way?

oers
  • 18,436
  • 13
  • 66
  • 75
jcrshankar
  • 1,165
  • 8
  • 25
  • 45

3 Answers3

1

Make the file available in classpath of the app and then use

classLoader.getResource("/path/in/class/path")

it will give you the location of file.

Note: the file needs to be out of archieved file. otherwise you need to extract it using code like this

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • java.net.URL imgURL = getClass().getResource("/a.doc"); i have used like this... its getting open ... but failed to open when i converted into jar file... – jcrshankar Nov 17 '11 at 13:47
  • as you mention above link i found code like FileUtils.openOutputStream etc... do we need to load in that way.. – jcrshankar Nov 17 '11 at 13:51
  • yeah it simply copies the file from jar to File system as file – jmj Nov 17 '11 at 13:59
0

Use *NIX notation:

/SRC/shankar/a.doc
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
-1
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
    desktop = Desktop.getDesktop();
}
String drive = System.getenv("HOMEDRIVE");
desktop.open(new File(drive + "\\SRC\\shankar\\a.doc"));
hurtledown
  • 673
  • 6
  • 18
  • Hurtle, this depends on an environment variable that is not present by default on non Microsoft Operational Systems. My Ubuntu distro for instance returns `null` for `System.getenv("HOMEDRIVE")`. Since there are available portable solutions I would greatly discourage your answer. – Anthony Accioly Nov 17 '11 at 12:45
  • System.getProperty("user.home") – Joop Eggen Nov 17 '11 at 12:54
  • yes, but also the string "c:\\SRC\\shankar\\a.doc" that is in the question is not going to work in your Ubuntu distro!!!!! – hurtledown Nov 17 '11 at 12:57
  • That's what we are here to fix right? :D. I mean, if the code was written in a portable way the OP wouldn't have it's initial problem to start with. Environment Variables should be used as last resort, if there are more straightforward ways to achieve the same result in a portable way, why are you suggesting a solution that isn't? – Anthony Accioly Nov 17 '11 at 13:16