tags:

views:

355

answers:

6

I am trying to use the Desktop API to launch the appropriate app for a file. So i am using this :

if (Desktop.isDesktopSupported()) 
        Desktop.getDesktop().open(new File(path));

where "path" is a String pointing to the file.

Everything works fine until i try to launch a jpg that resides at a network location (for instance "\\MyNet\folder\image.jpg") when i get an IOException :

java.io.IOException: Failed to open file:////MyNet/folder/image.jpg

Any one knows if there is a way to fix this?

A: 

file:////MyNet/folder/image.jpg is not a file path. It's an URL.

Marian
ok let's say that i have this path "\\MyNet\folder\image.jpg" which points to a network folder. Can i open the file? and how?
Savvas Dalkitsis
+1  A: 

I believe you need to specify the file location/name in standard URI format - which is close to the standard format except for servers. See the javadocs for the URI Class for more information.

At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax

[scheme:]scheme-specific-part[#fragment]

And a little later:

A hierarchical URI is subject to further parsing according to the syntax

[scheme:][//authority][path][?query][#fragment]

so the URI should look something like the following:

file://MyNet/folder/image.jpg

where "file://" is the protocol, "MyNet" is the server, and "/folder/image.jpg" is the directory location under the share.

Hope this helps a little.

aperkins
The main problem being that your URI has four slashes after the file:, while it should only have 2
aperkins
please check out my edits...
Savvas Dalkitsis
I'm sorry - I am not sure which edits you are talking about.
aperkins
:D i deleted them and created a dupe. :D check out the comment to find it and vote this closed. Thanks for your answer btw. i voted up.
Savvas Dalkitsis
correct that... i can't vote because i changed my old vote and now it wont allow me...unless you edit the answer so i can.. if you want to let me know.
Savvas Dalkitsis
<shrug> Whatever. I looked at the other question - it does look like a bug, as another person posted. Good luck with it - I hate those types of bugs! :)
aperkins
A: 
    File f = new File("\\\\192.168.0.4\\mybookrw\\save\\command.txt");
    Desktop.getDesktop().open(f);

Worked fine for me. The one caveat is that you have to be authenticated against the share already. If you paste the path into the run box and it prompts you for a username and password then its not going to work from an app.

Jherico
hm... my path is similar to this. it starts with \\\\ followed by the server name followed by the folder structure. And it doesn't work... Maybe because my path has spaces? does it work for you with spaces?
Savvas Dalkitsis
please check out my edits...
Savvas Dalkitsis
A: 

Everyone so far has assumed that the file isn't being found.

However, looking at the Desktop open() function, an IOException is thrown

if the specified file has no associated application or the associated application fails to be launched

Now, having said that, what happens if you open a jpg on your local machine? Also, what happens if you try manually launching the jpg through the network?

Edit: Actually, the problem may be that the default program set to open jpg files doesn't understand file:// uris. Sticking with UNC paths might be a better choice.

R. Bemrose
A: 

This has been answered appropriately on my dupe of this question. This is the answer :

http://stackoverflow.com/questions/1363003/not-possible-to-launch-a-file-on-a-network-using-java-desktop/1363056#1363056

Savvas Dalkitsis
A: 

I'm trying to use the Desktop class in client/server application. I Wanted when client click on button is open your default browser. But what happens it is when the client clicks on button the browser running on the server. How can I fix it?

Thanks

Duarte
this is a question. you should post is as a question. :)
Savvas Dalkitsis
Sorry! I already post it as a quastion. If you want you can delete this answer.Thanks
Duarte