What I need is to instruct OS to open the file with the default program used for that file type. Exactly as if, i.e., that file was double-clicked by user under Windows.
The purpose is, i.e., "your PDF file was generated. Click here to open it".
In platform-independent way, if possible...
I dont know exact terms for what I want, so if someone could update tags, I'd most appreciate that ;)
You need the Desktop class, and the open() method in particular.
Launches the associated application to
open the file. If the specified file
is a directory, the file manager of
the current platform is launched to
open it.
Since Java 6, we have Desktop.open() for exactly that purpose.
Related
I made a text editor in java. It has a FileExplorer class which allows me to read and write to a file. Now I would like to know how I could open said file (text.txt) with my editor application from outside my application. Basically when I double-click on the file (text.txt) it should start my application and pass some variables(like name and path). The application is a .jar file.
I am not going to bother anyone to go through the 2k lines of code, so I won't post it in here. But it is just a JTextPane in a JFrame and a PrintWriter/BufferedReader reading and writing to the file.
PS: should preferably work cross-platform.
Thanks
This question is not about java. It is about mapping files to specific application using you OS tools.
If you are on Windows you have to map extension *.txt to your application. Take a look on this article for details.
Please note that your application must accept file path in command line.
To make association easier I'd recommend you to writer batch file that runs your application and also accepts file name in command line. Then you just have to associate your batch file with *.txt extension.
If you are on Linux association technique depends on your flavor, but you can google it. Obviously you will have to create shell script instead of batch file.
EDIT
Actually your question is mostly about installation process. There are a lot of installation tools that can do this work for you. Some of the tools are even cross platform (I can remember "install anywhere"). There are both commercial and free tools that do this.
so my program is going to need to crate a file with login information. After that, everytime the program will just run normally, unless the login file gets deleted some how. I want to know of a quick way that I can do this in a multi OS way. I dont want to save the file to a specific part on the disk, because then I have to do it for every OS. What else can I do?
You can use System.getProperty("home.dir") to figure out the user's home directory in a non-OS-specific way
Read more about this on http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
Depending on what you want to store, you may not need a file at all. See
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/prefs/Preferences.html#userRoot%28%29
or
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/prefs/Preferences.html#systemRoot%28%29
I am using JNA with Java to find some properties about open windows on a Windows machine desktop. I am trying to find a way to get the file being accessed by an arbitrary windowed application. For instance, say I get information regarding the window of an open pdf document in adobe. I want to be able to get the filepath of the pdf document displayed in the window.
I know about the GetWindowModuleFileName() method, however this gets you the filepath of the executable of the application, i.e. 'javaw.exe'. If you have 'my.pdf' open in adobe, I'd like to get the filepath of this document, i.e. 'C:\...\my.pdf'.
I've done some searching around (on this site and others) and haven't found anything yet on this in particular.
thank you for your time, -Kevin
If you know the process ID, you can get the list of all files currently opened by the process using Handle utility. However, it depends if adobe reader continues to keep the file open or closes it after reading it completely.
I want to create my own file format for a particular kind of file. When someone downloads this file I want their system to know it should be opened with my application.
For example when I download a .doc file, my computer asks me whether I want to save the file or open it with Open Office. Similarly, If that .doc file is sitting on my desktop, and I double click it, it automatically opens with the correct application.
I believe this has to do with associating the file extension with the application in the context of the underlying OS.
Can any one point me to some good resources about how to do this in java?
Thanks.
Edit:
Sorry I want to clarify. Is there a way I can have my application associate the file type with itself when it is installed?
Edit:
found this...
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/
platform independent solution
This source shows how to make a file association in windows:
http://www.rgagnon.com/javadetails/java-0592.html
You will probably have to do it per installer that you make in each OS.
found this... http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/ platform independent solution
I have a Web Start application which needs to save a single text file. I would like to know where I should write the file, such that it goes in a tidy place regardless of the platform it runs on.
If I get my filepath like this...
new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "test.txt);
... then the file gets written somewhere nice and inconspicuous on Linux, but on the desktop on Windows, alongside the icon I'm running the program from, and that's not ideal because the user shouldn't necessarily need to know or care that the file exists.
Is there a good, platform-independent way of getting a file path that allows me to save a file somewhere discreet? I don't much care where it ends up, providing it's not in full view on the desktop.
Thanks.
Neil
user.dir is the current working directory; use user.home instead and java.io.tmpdir for temporary files.
Here's the list of predefined properties for Java 1.5.
What about using a .yourapp directory under user.home instead? You shouldn't face any permission problem, a windows user won't really notice it, it would be hidden on GNU/Linux.
For temporary stuff, you may prefer java.io.tmpdir but keep in mind that the content of this directory (/tmp on GNU/Linux) might be wiped out upon reboot (e.g. with Debian distros). So this is really for temporary stuff.
PS: I prefer to use File.separator over System.getProperty("file.separator").
I strongly suggest using the PersistenceService.
If you don't need the file per user or to be incredibly permanent you can use System.getProperty("java.io.tmpdir"));
As others have stated, "user.home" is a decent choice. If you can get away with it, Preferences are another good choice for storing certain things.
If you do end up using something like "user.home" then make sure you have a fallback position. I've been in many situations where a user's home directory was not writable (locked down windows installs under a domain server for example).
If you store data in the "user.home" directory then put it under a ".myApp" style sub-directory and first verify that you can create that directory. If you cannot create that directory then you could always prompt the user for a place to store it. And then store that location in a system Preference so that it's known the next time the app is run.