|
-
August 5th, 2009, 08:02 AM
#1
Creating a java program that acts like a normal windows executable?
I know you can compile java programs into a single .jar file that you can execute but I have a problem and I want to know if it can be solved.
Now I have a custom file format that has the extension .dta the files contain some data to create a graph. Now I want to be able to double click these files and have them open in my java program. Also if the program is already open I want any further files that are opened (not via the file > open functionality) to open in this same instance of the application.
How can I do this and what would be the best way. Should I use a separate launcher application written in say C# that calls the java -jar application.jar filename? Or is there an easier way to do this.
Thanks in advance.
-
August 5th, 2009, 08:18 AM
#2
Re: Creating a java program that acts like a normal windows executable?
That is an OS specific question. In Windows you can associate the extension with any type of executable program. In Explorer go to Tools-> Folder Options-> File Types and from there you can add the new extension if it isn't there already, and you can associate it with an executable program IF it is in the list. If it is NOT in the list... I can't help you. I am not sure how to add it to the associated file type list.
If this is linux/unix you can do a similar process. Depending on your distro, you can (in Ubuntu) select a file, right click -> properties -> open with and then you can select the application. Easier because you should be able to select the jar file, whereas in Windows it has to be in the master list.
Mac OS... no idea.
Now, I haven't tried this so I can't guarantee that it will work, just throwing out ideas.
-
August 5th, 2009, 08:59 AM
#3
Re: Creating a java program that acts like a normal windows executable?
I know how to add a file extension to windows and associate it with an executable however that is not the problem, how to make my jar file an 'executable' program is.
-
August 5th, 2009, 11:10 AM
#4
Re: Creating a java program that acts like a normal windows executable?
Whatever your main entry point of the application is, if you specify this in the MANIFEST.MF
(do a quick google search on manifest files), then when you export a jar from your project and execute it, it will attempt to execute the main method of that class file within the jar.
------
If you are satisfied with the responses, add to the user's rep!
-
August 5th, 2009, 11:20 AM
#5
Re: Creating a java program that acts like a normal windows executable?
This is a problem that has a couple of solutions. The problem is, as you have noticed, that a jar file is not an executable. It can be run because it is associated to java.exe and so double clicking on the jar file launches java.exe to run the jar file.
The solutions I'm aware of are:
1. Use a third party packager to convert the jar to a .exe file. This of course makes the code OS dependant.
2. Create a .bat or .cmd file which contains the command to launch the jar file and associate your data file to the batch file.
-
August 9th, 2009, 08:20 AM
#6
Re: Creating a java program that acts like a normal windows executable?
Ok I've settled on a batch file or a launcher written in C to load the jar file, now we have one more problem.
How do I open multiple files in the same instance? For example I double click a .dta (the file type I am associating with my program) file which is linked to my launcher (bat or exe), this then loads up my jar file with the .dta file as an argument, all loads as expected. I now double click a second .dta file and the same occurs, however it has now loaded two instances of my program. What I would ultimately like is to have both files open in the same instance (my program is a tabbed window with multiple graphs showing, file > open works as expected, however if a users double clicks multiple .dta files it doesn't.)
Now I've done some research and found an answer for windows using JNA or JNI or JWinApi to call the FindWindow function from the user32.dll. If FindWindow(NULL,"My Program's Title") does not equal NULL, then there is an instance open and so on. I will need to do some more research and lots of testing.
Now this method sounds fairly complicated, I was wondering if there was anything built into java that can simplify this problem.
Thanks for all the help guys, really appreciate it.
Last edited by rolls; August 9th, 2009 at 08:26 AM.
-
August 11th, 2009, 12:59 AM
#7
Re: Creating a java program that acts like a normal windows executable?
using JNI is easiest way, and perhaps only one. Java is not meant to replace OS calls, it simply has interface to them.
you could make executable but it is just going to be wrapper for java -jar yourapp.jar call
-
August 12th, 2009, 04:07 AM
#8
Re: Creating a java program that acts like a normal windows executable?
 Originally Posted by rolls
I know you can compile java programs into a single .jar file that you can execute
There's another alternative and that's to compile the Java program to a native executable (like a .exe). For this you need a Java-to-native compiler like Excelsior Jet for example,
http://www.excelsior-usa.com/jet.html
-
August 13th, 2009, 09:47 AM
#9
Re: Creating a java program that acts like a normal windows executable?
Cheers but Ive already settled on a batch file, got any ideas about my other problem ?
-
August 13th, 2009, 01:47 PM
#10
Re: Creating a java program that acts like a normal windows executable?
 Originally Posted by rolls
... got any ideas about my other problem ?
What other problem? If you're referring to being able to click on new files outside the Java app and have them load in the same app, AFAIK there is nothing in Java that will do this easily.
However, you can get fairly close with drag and drop between Java and Windows Explorer - see Drag And Drop between JList and Windows Explorer - note there is a small bug - line 55-56 should read:
Code:
String name = listModel.getElementAt(0).toString();
They know enough who know how to learn...
J. Adams
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 16th, 2009, 08:59 AM
#11
Re: Creating a java program that acts like a normal windows executable?
I'm going to write an extension which uses hooks to user32.dll (obviously will only work for windows) The hook will search for instances of my java program already in memory, if they exist it will send the correct command to the current instance, if it does not exist it will start a new instance.
Shouldn't be too hard from what I've seen, probably the hardest part will be identifying my program from other ones. Normally you'd just search for an instance of 'program.exe' but being as all java programs open with java.exe I might have to do some research.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|