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)
Related
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.
I am trying to implement a simple encryption utility for educational pourpuses, it works, at least with simple files, but when I succesfully encrypt one file, i'd like not to encrypt it again, because that could lead to loose my data if i encrypt/decrypt it in a wrong way... Is there a way to prevent me from do it?
I am using java, and the default encryption library.
Thanks in advance
The answer to what you want to know here depends very much on how your encrypting the files in the first place.
I'll list a couple of different approaches that might help you however
Approach 1 - Scripting
If your using a 3rd party tool such as an encryption util written by another java programmer, and if your running this tool in some kind of a shell session, your best bet might be to wrap the invocation of said tool in a shell script.
If your running on windows this could be a batch file, on Linux a bash script.
Essentially you use this approach by working out ahead of time what command you need to use, then putting that command into said shell script while substituting any parameters you need to change.
Following on from the wrapped command you could then provide further commands to rename the file, or embed some kind of information in it's file properties or file name, a possible example might be something like:
IF NOT EXIST %%0.encrypted(
encrypt %%0 -a -b -c -d
rename %%0 %%0.encrypted) ELSE (
)
ELSE
()
NOTE: These are just theoretical examples as I don't know what your OS is
If this was saved in a file called 'myencrypt.bat', then you could just type
myencrypt.bat afile.ext
Approach 2 - Custom Bytes
If you have direct control of the source code and consequently the application that performs this encryption, then why not make a pseudo file format.
Add some kind of a marker into the file that your program then checks for.
By way of an example, you could perhaps
add the following string to the front of the file
ENCFOriginalFile.Ext
Turn that into a set of bytes, then load the file in, encrypt it, add the bytes from the text on and save it back out, maybe with a custom file extension.
When you come to encrypt a file again, all you then need to do is read the first 4 bytes and if they are equal to ENCF you know that the file is already encrypted.
Those are just 2 ideas I can think of off the top of my head, but it's late here and I'm tired. If I was more awake I could probably come up with a whole page full.
Since it is encrypted it cannot be opened in the default program for that file type, so you can savely rename the file. This can be done for example by adding .enc as the extension. Doing so will make it easy to spot the encrypted file for you and your java application.
Depending on your use case you can also let your java application manage a database of encrypted files.
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.
Ok, the question is quite simple.
I have a Java program, which I am extracting some save-files to an external location.
Right now, I use C:/ApplicationName, however, that is a very bad way to do that.
I know alot of locations i could use, for instance:
%Appdata%
C:\Program Files (x86)
C:\Users\Users\Documents (Ive seen some indie games use this.)
Other locations?
But I can't figure out, when to use the proper one.
And if i want to support Linux and OSX, is there a libary, which supports that, or do i manually have to wrap them into an if/else with System.getProperties("os.name")?
Keep in mind that sometimes when games save to a specific directory, they aren't built to be cross-platform.
There are two ways you could do this. The first (and personally my preferred version) is to save in the directory of the application. This also has the advantage of being portable. However, the saves are linked to the application, so if the user deletes the application and reinstalls later they'll lose their data (which may or may not be a good thing).
Another option is to use System.getProperty().
Specifically, you can use:
System.getProperty("user.dir");
System.getProperty("user.home");
System.getProperty("user.name");
To figure out where to put your files.
This will be a lot more complicated, so the first method is preferred.
If it's data the user does not need direct access to, such as stored application/game state data, then it's appropriate to store it in the user's application data directory. Try this:
1) Determine the OS.
2) Get the user home directory.
System.getProperty("user.home");
3) Append OS-specific application directory:
Mac: /Library/Application Support/MyApp
Windows: \\Application Data\\MyApp
Linux: MyApp (there's no convention here that I know of)
If the data needs to be exported, such as saving a document from your application, then ask the user where to store the file via a file dialog, defaulting to their Documents directory.
Disclaimer:I know that this is something that is covered very early in learning Java, however, I am just a hobbyist and I am self-taught. I am only as good as the information I find online is. Please do not be derisive or condescending. We were all new at one time.
As mentioned, I have a written a Java Form app. What it does it takes user input and writes it to an Excel file. How can I dynamically reference a file (the Excel, in particular) that is on each users' computer, found in a different directory but not have to hardcode each user's exact file path and distribute individually?
Thanks!
I think you are looking for user.home property.
System.getProperty("user.home");
Which will give you, say, c:\users\mk
and then you can append your path to that.
You could have the application ask the user for the path.
As part of the application configuration have the user define an environment variable using a known name with either the desired directory or the install directory (then use the variable to derive the desired directory).
for example:
tell the user to define BLAMMY_HOME which contains the install location of your software (named BLAMMY).
derive the desired directory by concatinating the value of BLAMMY_HOME and "/desired/directory/name".
or
have the user define BLAMMY_SPOT which contains the full path the the desired directory.
use the value of BLAMMY_SPOT in your application.
Defaulting to using user.home is fine, but I (personally) do not like that technique (as a user).
Take an example of 2 users
UserA path: /opt/file/directories/target (contains the excel file)
UserB path: /opt/directories/target (contains the excel file)
There's absolutely no way to find the target directory (except searching for it, but then you might find another one) unless the application has access to some out of band information. For example, Java offers the user.home property
String pathPrefix = System.getProperty("user.home");
so you can use that and make your target directory relative to that.
That's the whole purpose of applications having installation/working directories. As another example, take the Windows Registry. Imagine you had to download a patch from the internet. The patch itself wouldn't be able to check all the paths on the file system until it found yours (each user has a different one). Instead, it can find that path from the Windows Registry (or something comparable depending on the application).