Java Find default desktop application for extension - java

I am looking for a way, using java to find the default application for opening a certain extension. For example, I have Gimp set to open all .jpg files. So when I evaluate xyz.jpg I want to be able to find Gimp. I DO NOT want to open the file with the application I just want to know what it is.
I tried sifting through the registry and Gimp was not the default it was windows photo viewer even though the desktop recognizes it as Gimp.
I also tried using assoc and ftype from the command line and it also returned windows photo viewer. I assume this is because it is just looking at the registry.
Note: I do not want to change the default application in the registry I just want to know how I can find what the desktop finds as the default application for a certain extension.
Matt

I found the entries for user defined applications in the registry. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts.
I then loaded all these entries from the registry and grabbed what I needed.

Related

How do I compute the file path for a user-written settings file in Java (JavaFX) that is compatible with Linux, Mac and Windows?

My JavaFX program needs to compute a file name path for various user-written files such as the product-specific preferences. For example, "Do you want to open your previous file the next time the program starts" and so on. I have successfully experimented with https://github.com/dlemmermann/JPackageScriptFX and "jpackage" for Windows at least, so it looks like I will shortly need a way to code, in a cross-platform manner, the "correct" path in which to store such files.
Is there a standard API or coding technique that will give me a file path that the program can write using user permissions that is "correct" for these native platforms?
I am not aware of a single piece of software which would do that but I think my answer to this questions Java - Cross-platform filepath may be helpfull for you. It also mentions how the same can be achieved on Android for example.

Passing string/path arguments on my main program to open image files

I'm trying to make a program similar to Windows Photo Viewer, the program that pops up usually if you double-click an image in Windows. I've already made my own program, however it uses a JFileChooser associated with a button to bring up images to open inside the application itself (Windows photo app doesn't seem to do this).
Question:
How can I make my application handle certain file types when its double-clicked from Windows? My guess would be something along the lines of my main class having a Path/String type argument of the file I want to open, but I'm not sure how to exactly implement that.
What type does Windows pass to a program whenever we open a file? String?
Once my first question is fixed, is there anything special I would need to do to associate my program to image files (when making .exe file or installer)?
Include a java-web-start <association/> tag as a "hint to the JNLP client that it wishes to be registered with the operating system as the primary handler of certain extensions and a certain mime-type." Note that " the <offline-allowed/> element must also be included." A web server is not required and a platform-specific <shortcut/> is optional. The file to open will be a command line parameter.

Get Windows files associations via Java

I thought this would be an easy task but . . .
I want my Java program to get the user's Windows file associations.
In other words, I want to know what the user uses to open .txt files, .cvs files, etc.
The assoc and ftype commands provide that info, but not for the user.
In other words, if I've set my text editor to Notepad++,
assoc and ftype don't show it. They show the system default, Notepad, instead.
It looks like I have to get that info from the registry but I have two problems.
1) I don't know the exact registry keys I want to pull
(though I've looked at "reg query HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, etc.)
2) I don't know how to pull the key from the registry. I've seen JNI mentioned
but haven't figured out the details.
Any hints appreciated.
In Win7, you can find the "class"* for each file extension in the
HKLM\SOFTWARE\Classes\<extension>\(Default)
key.
For example, on my machine
HKLM\SOFTWARE\Classes\.txt
has a (Default) key of txtfile.
In that same path, you can find what opens files of the class txtfile:
HKLM\SOFTWARE\Classes\txtfile
Which should have a subpath of ...\shell\open\command
On my system,
HKLM\SOFTWARE\Classes\txtfile\shell\open\command\(Default)
is
%SystemRoot%\system32\NOTEPAD.EXE %1
Which you could parse to find the executable that opens .txt files.
For user-specific customizations, you can replace
HKLM\SOFTWARE\Classes\
with
HKCU\Software\Classes\
All that being said, I like MadProgrammer's suggestion if possible for your application.
* I'm sure there is a better name for "class", I just don't know it.
Take a look at the Eclipse's Program class, which should to what you want: http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fprogram%2FProgram.html.
If you would like to launch the program, use the Desktop-class (like MadProgrammer suggested)

opening file with java application automatically

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

How to preview a file on the server in JBoss

I need some ideas on how I can best solve this problem.
I have a JBoss Seam application running on JBoss 4.3.3
What a small portion of this application does is generate an html and a pdf document based on an Open Office template.
The files that are generated I put inside /tmp/ on the filesystem.
I have tried with System.getProperties("tmp.dir") and some other options, and they always return $JBOSS_HOME/bin
I would like to choose the path $JBOSS_HOME/$DEPLOY/myEAR.ear/myWAR.war/WhateverLocationHere/
However, I don't know how I can programatically choose path without giving an absolute path, or setting $JBOSS_HOME and $DEPLOY.
Anybody know how I can do this?
The second question;
I want to easily preview these generated files. Either through JavaScript, or whatever is the easiest way. However, JavaScript cannot access the filesystem on the server, so I cannot open the file through JavaScript.
Any easy solutions out there?
Not sure how you are generating your PDFs, but if possible, skip the disk IO all together, stash the PDF content in a byte[] and flush it out to the user in a servlet setting the mime type to application/pdf* that responds to a URL which is specified by a link in your client or dynamically set in a <div> by javascript. You're probably taking the memory hit anyways, and in addition to skipping the IO, you don't have to worry about deleting the tmp files when you're done with the preview.
*****I think this is right. Need to look it up.
Not sure I have a complete grasp of what you are trying to achieve, but I'll give it a try anyway:
My assumption is that your final goal is to make some files (PDF, HTML) available to end users via a web application.
In that case, why not have Apache serve those file to the end users, so you only need your JBOSS application to know the path of a directory that is mapped to an Apache virtual host.
So basically, create a file and save it as /var/www/html/myappfiles/tempfile.pdf (the folder your application knows), and then provide http://mydomain.com/myappfiles (an Apache virtual host) to your users. The rest will be done by the web server.
You will have to set an environment variable or system property to let your application know where your folder resides (/var/www/html/myappfiles/ in this example).
Hopefully I was not way off :)
I agree with Peter (yo Pete!). Put the directory outside of your WAR and setup an environment variable pointing to this. Have a read of this post by Jacob Orshalick about how to configure environment variables in Seam :
As for previewing PDFs, have a look at how Google Docs handles previewing PDFs - it displays them as an image. To do this with Java check out the Sun PDF Renderer.
I'm not sure if this works in JBoss, given that you want a path inside a WAR archive, but you could try using ServletContext.getRealPath(String).
However, I personally would not want generated files to be inside my deployed application; instead I would configure an external data directory somewhere like $JBOSS_HOME/server/default/data/myapp
First, most platforms use java.io.tmpdir to set a temporary directory. Some servlet containers redefine this property to be something underneath their tree. Why do you care where the file gets written?
Second, I agree with Nicholas: After generating the PDF on the server side, you can generate a URL that, when clicked, sends the file to the browser. If you use MIME type application/pdf, the browser should do the right thing with it.

Categories