This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Not possible to launch a file on a network using Java Desktop?
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?
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.
file:////MyNet/folder/image.jpg is not a file path. It's an URL.
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.
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.
Related
I am working on a java application, it’s has a trial version.
I need to save all informations to activate the application in an xml file ( such as expired day and application ID ), i need to save this file in an absolute path that will be the same for all pc.
Can u help me please?
Don't. It's very bad practice to use absolute paths for anything.
Why not save the XML file either to a folder specific to the user (if your licenses are bound to people), or to a folder relative to your application (if your licenses are bound to a machine, as your question seems to indicate)?
I agree that using absolute paths may not be the way to go but to solve your problem you could get the name of the user like so
String username = System.getProperty("user.name");
From there you can use the windows file structure to get to the documents folder
String documentsFolder = "C:/Users/"+username+"/Documents";
With that directory path you can read and write to that folder. This folder structure would only apply to windows and would need to be changed for Mac or Linux machines, but the username can be obtained the same way.
Just a suggestion. If you are supporting windows machines only maybe you can create a registry with the start and end date of the application and use that as a reference point which will be consistent even if the application is uninstalled or re-installed. Also maybe creating a System variable with the date could be another way to go. Just a few thoughts.
Hope this helps
I want to know if there is a way of opening mp3 files using either Windows Media Player and VLC Media Player.
I have the path of the file saved as a String and was wondering if I can use this to open the file on either of the applications mentioned above.
I understand one method of doing this is by using the Desktop class but this opens the file using the default application, which is Windows Media Player in my case. How would I open the file in VLC media player?
try
Runtime.getRuntime().exec("<your vlc path> <your file>");
I.E.
Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\VideoLAN\VLC\\vlc.exe\" abc.mp3");
Tried and it works
Judging by the Desktop.open apidoc and
Desktop tutorial it looks like the open method can only open the file in their default associated program.
So, you have (maybe) two options here:
access the registry and change the default association (not a nice one)
might try giving a URI and opening it via browser, which might result in opening the file in desired program. However, it looks like the file scheme is the only acceptable solution, and that will most probably open in the default program. There is a mms scheme but it is used for video-streaming, and again, it will most probably open in the default program.
Here is a link to the Runtime.exec solution for windows, linux and osx variant.
You should know the path to the VLC and/or WMP. Then you can use Runtime class for this.
Process p = Runtime.getRuntime().exec("c:/vlc/vlc.exe " + mp3FileName);
I am working on an Eclipse Web Dynamic Project, and trying to access a file that exists in my local machine/server.
What I am looking for is something like "base_url()" in CodeIgniter, which automatically points to the directory the server is located.
I am using a Mac.
try{
model.read(new FileInputStream(url),"");
}catch(IOException e)
{
System.out.println("Exception caught"+e.getMessage());
}
This is the part of the code I am working on, which I am trying to feed the correct URL path to read.
After searching StackOverflow and other places, I came across this piece of code:
String url = request.getRequestURL().toString().replace(request.getRequestURI().substring(1), request.getContextPath())
+"/WebContent/WEB-INF/test.xml";
Which did not seem to work.
I then tried to hard code the path directory in, only to realise that I dont know how Mac file systems work :/
Can anyone share some light on this?
Thank you in advance
I did not understand very well if you are using a servlet or something else (like a WS), however the request object exposes a method called getRealPath() which gives well... the real path of the servlet's context in your file system.
So you need to change your code with this:
String url = this.getServletContext().getRealPath("")+"/test.xml";
It has nothing to do with OS, as you surely know java is portable.
I think what you want is
String path = getServletContext().getRealPath("/");
which point to the bas directory of your application in the server
e.g. /opt/tomcat/webapps/MyApp
I always use getResourceAsStream and suck it up off of my class path. Flat file names are problematic inside of web applications.
i am checking the text file which is present in the mapped hard drive or not.
File cfile = new File("R:\\Link Fixer Reports\\ServiceTest.txt");
but it shows that file is not present
when use c:\\t.txt
it shows the file
what is the problem and how can i rectify the problem?
It's likely that the user that your code is running as can't see the mapped directory.
You've tagged this question as java-ee so I'd guess that this code is running within a web service or similar? What user is your application server running as? Verify that this user can access the location. As #Christian pointed out, a UNC-Path is a better way to go - just make sure that you can access the network location. Try runas net use to double-check.
This Java code lists files in a directory on a Windows shared drive. Will it work correctly on a Unix system?
File directory = new File("\\\\server/Shared/stuff/mystuff");
for (File file: directory.listFiles()) {
System.out.println(file);
}
Short answer: No.
Long answer: Do you have samba installed? Even then you need to mount the the share. So it probably won't work.
EDIT
Java delegates the call to the underlying OS eventually. Since Unix doesn't know what the \\SERVERNAME path means, Java doesn't know what it means either. What you have to do, to get this to work is mount the drive explicitly using Samba. Your other option, if you are running Ubuntu, is look under .gvfs in your home directory. Ubuntu creates a mount there for your Samba shares, which you should be able to access using Java. If you don't want to rely on external tools, try JCIFS for a pure-Java solution.
No...
Just let the user select the right path and use an OS dependent file-selection dialog.
On my system (Debian Sid with Gnome 2.30 Desktop) I have to select "smb:///server/Shared/..." to achieve the same behaviour. I think, that GVFS (Gnome Virtual File System) using smbfs drivers handles the real connection in the background...
No, as that is a UNC Path, which is a windowsism.
Are you trying to access a windows share from unix? Then have a look at jcifs.
The counter question I get when seeing this is: "Why would you want to hard-code a path in your application?"
Even if it was just for the example and you intend to load the path from a property file or anything, I still think you are on the wrong track here.
First of all you will want to avoid absolute paths like the plague. Relative paths are sort of ok. You can use slash ('/') characters in paths hardcoded, it will work on both Windows and Linux/Mac. Basically all platforms.
Second of all, why use paths at all? This is the internet age. Use URL's! file: URL's will accomplish the same thing as file paths, but using URL's make your app accept resources from other sources such as web sites and FTP as well.
Third of all, avoid the File class. If you invent a good way to do that, you are out of the woodworks completely. Use URL's together with getResource and getResourceAsStream and your app will work platform independent and across network boundaries over the internet.