Utilising a file association in a Java application - java

I am in the process of writing a cross platform Swing based application in which I want to utilize a file association which has been registered with the OS.
So iv got to the point where I can click on a file and my app loads, but what I need to know is how I can get my application to know where the file is that launched it and then query the contents.
Is there something further I have to do with the file association registration? Or can Java do this for me?

I'm not positive, but I'd expect that the name of the file you're processing by file click will end up in the arguments to your main() method. Have you tried/checked that?

If this is on Windows (you didn't specify):
In the registry wherever you specified your application path for the file type registered to it, add to "%1". This is a special parameter Windows will fill in with the path of the file that was clicked. So your registry entry would look something like c:\path\to\app.exe "%1"

One way to do this is to have the file association run your Java app via a script or batch file, and have the batch file pass the pathname of the file as a command line argument, environment variable or Java property.

Extensions can be linked to applications, you can setup the registry keys during installation. Which keys you need is documented here:
http://support.microsoft.com/?scid=kb%3Ben-us%3B185453&x=6&y=11
From java you can't access the windows registry in a direct way.
Using Runtime you could do something like that
http://www.rgagnon.com/javadetails/java-0480.html

There're two commands on Windows that can help, assoc and ftype, so that you needn't do the dirty laundry to manipulate registry. Invoke the commands using, say, java.lang.Process. http://www.rgagnon.com/javadetails/java-0592.html

Related

Changing file associations using Java

I have read that you can change the Windows file associations by using commands like assoc .pmdtb="X:\PassswordManager.bat", but I still have some problems with it.
When I run the command in the command line, then nothing changes. Files with this extension are still opened with Notepad.
When I try to run the command in Java, I am denied access.
How do I edit the Windows internal file associations in Java? If I should rather edit the registry directly, which of the many Classes directories?
My Java code:
Runtime.getRuntime().exec("cmd.exe /c assoc .pmdtb=\"X:\\PasswordManager.bat\"");
// --> Access denied.
Thanks in advance.
File associations will apparently no longer be changeable with assoc and ftype starting with Windows 8. A hash code is additionally in the registry, which is generated with a secret algorithm. If I change the file association, but the hash does not match, the default opening of this file extension will not change. The Windows algorithm for the hash has been reverse engineered. Based on this, you could translate the algorithm into Java and change the registry directly.
The C++ code with QT can be found here: https://pastebin.com/yVhWeQ3X
By far the simplest method is to use one of many FTA utilities that resolve the security hash for you.
For example https://danysys.com/set-file-type-association-default-application-command-line-windows-10-userchoice-hash-internal-method/
Then use that via a cmd line call from your java app
You may need to test out the options (read the comments on linked page above and review issues on github) however it could be as simple as
SFTA.exe --reg "X:\PassswordManager.bat" "pmdtb"

Change working dir in Java Webstart

Is there a way to change working dir for JVM when running Java Webstart?
When i use system.setProperties("user.dir", newDir) it sets it(system.getProperties() shows it does) but ignores it.
Is this a limitation in Java Webstart to always use the working dir where i started the jnlp file?
I am using all permissions in the jnlp file.
Please help!
EDIT: Whatever i do now, my webstart always uses user.dir to save files. Frustrating!
I've had this question in the past myself, but I've always found that, in the end, I didn't need it. Why do I say this?
Because your java web start app is not like an executable run from Program Files. It doesn't even exist on your computer like most programs (it is broken up into a bunch of different files and reassembled by the JVM). Therefore, you cannot say that the program has its own directory to do what it needs.
But it doesn't need to. Here's why:
Java has the Preferences API to help when you need to store data. The under-workings of the Preferences API is as mysterious as JWS, thus they are really a perfect fit. Either way, if you need to write things to a file, you should check this API to see if it can meet your needs.
If you need to write files for the user, then prompting them and allowing them to choose the location obviously means you won't use your current working directory to read/write files.
If you need to serialize objects, you should just create a program directory using the user.home resource as #AndrewThompson suggested. This will be "your" directory and is as good (in fact, better) than a directory in Program Files (if you're running on Windows, as an example).
In conclusion, in all cases (that I've come across), there's no need to change your current working directory. If you need your own folder, create one in user.home (because you won't run into file permissions issues there).
..all my settings file i use is created in the user.dir.
There is the mistake. Put them in a sub-directory of user.home & the problem is solved.
In the hypothesis you really really need to divert user.dir property for Java WebStart execution, here is the only option I have found: set this system environment variable (so system wide):
_JAVA_OPTIONS="-Duser.dir=C:\Temp"
But care about it, this option is read and applied to any JVM executions.
Why was it required in my context ? Because Java WebStart ClassLoader was looking for any single resource (class, properties...) in user profile before getting it from jar files in cache. As the user profile has been moved to a network storage, application start up became terribly slow. I am still investigating Java sources to understand (and avoid) this behavior. So my applications work perfectly without setting user.dir but that was the only work-around for the performance issue we got at the moment.
The recommended way to pass runtime parameters or user specific setting is through the jnlp argument
<application-desc main-class=".....">
<argument>user.home</argument>
..............

Install fonts from a java program

I would like to install a set of fonts to the windows system from my java class. I am using these font for my Birt Report.
You can write a batch/powershell script and include it, along with the font files in your application. Then you can execute the script with
Runtime.getRuntime().exec(...)
You'll most likely have to raise the privileges for your application once you run it.
As for passing the password. It's possible to run cmd.exe so it pops up and propts the user for it. You can also try assigning the return value of exec to a Process class object, which has InputStream and OutputStream properties. I'm not sure how to do it properly. I did it once in a project, a couple of years ago but I no longer have the code.
If you only have to install the fonts once, consider creating an installer for your java application that will take care of it. There's a neat installer generator called IzPack, which allows you to create complex installers using XML. It also allows you to raise privileges for executables run during the installation. This is the way I do such stuff.
You can install those fonts user System -> Fonts if you are in window to test it out.
If you are trying to arrange it with your program, you must start by including them in your java resource file in order to refer to it later on.
Hope it helps~
Without local admin rights it is possible to add custom fonts to the font cache. Then your custom fonts will be available to all your applications until you log out.
The Windows API that does that is AddFontResource. Via a JNI helper DLL you can call it directly, or just execute the RegisterFonts utility.

associating a custom file extension with java app in windows

I would like to "link" few file extensions to my java application under windows. When user double clicks file with "linked" extension, I would like to open my app and I need to know path to file that launched app.
If you deploy the app. using Java Web Start, an interest in file-types can be declared in the launch file. See the demo. of the file services, which..
..prompts the user to associate file extension .zzz (simply a file type unlikely to clash with existing file associations) of content type text/sleepytime. ..
When the user double clicks a .zzz file, it should open in the app. Actually, the word 'prompts' there is not the whole story. If you launch the sand-boxed version you will be prompted as to associating the file-type. The trusted version does not prompt.
To add more user-control to the process, look to the IntegrationService that was introduced in 1.6.0_18 (I don't have a demo. of that one yet). You might run it at start-up, after checking with the user.
this would have to be done during installation. how are you planning on letting your user install your application?
you have to realize at this stage that you just made things a whole lot more complicated. registering file extensions means meddling with the registry. what happens if the user doesn't want your application anymore? or moves the file that launches your application?
you'll have to pick an installation creator. here's a so question about that: https://stackoverflow.com/questions/3767/what-is-the-best-choice-for-building-windows-installers
and then you'll have to learn that installer creator's language. here's how what you're asking for is done in nsis. remember that the script takes care of questions like "if the user uninstalls my application, and i didn't change the file associations at install time, should i then remove these file associations on uninstall?" so it's a bit long. here it is anyways: http://nsis.sourceforge.net/File_Association
maybe it can be done in an easier way in another installer creator.
however, in this example, you give the register function of nsis the start command for your application, and then it adds %1 to it in the start command of the windows file association. so you should give it the start command
javaw -cp installpath\yourcode.jar package.name.MainClassName
and then things should work out. this will take some experimenting of course, and you will have to be quite sure about how to start your application from the command line.
good luck!

How to restart the Java Process with -Djava.library.path?

I have an application that is started with JWS. The first time user launches this application he has to choose a path where Berkeley DB XML is installed. I do need this to set the native library path and restart the application with -Djava.library.path parameter. Berkeley DB XML java bindings uses JNI to make calls to the database. Since our users may have different OS I cannot rely on a default location.
So, I have a problem with getting current classpath. When I print out "java.class.path" it only gives me "/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/deploy.jar". I have three jars that I cannot find in my sys props.
on my Mac.
I hope this was understandable and thanks for any tips beforehand.
Try to repaire permissions with DiskUtil.
Avoid using this pattern. What you could do is to store the command and execute that simulating that you are starting a new process.

Categories