including an exe file to jar - java

I have written a java program that is actually works as a gui to an existing command line program.
so in basic all my program does is Runtime.getRuntime().exec("myprogram parameter");. So I have exported my java source as a executable-jar file by using Eclipse IDE and it is working nice, however I indeed need to include this myprogram.exe to the directory of the generated jar file inorder to work.
Now I am looking for a way to include myprogram.exe inside the jar file so I can keep it bundled as a single file, a method using using Eclipse would be preferred.

You can simply jar it up as an extra resource (like a .css, .properties etc.).
When your Java program runs, extract the .exe using Class.getResourceAsStream() and write it to a temporary directory in order to run it from there (since you can't run it directly from the .jar file).

Related

Running GUI Application without IDE

Lets say that I built a GUI Application using NetBeans. To run this java application I need to open source code in IDE and then run. I know that I can also run through command prompt.
But how do I start the application independent of IDE. Isn't there some .exe file or something like that, which on double clicking directly runs the application?
If not, how do I generate such a file?
Here you can find how to create .jar in Netbeans: How to create a Jar file in Netbeans
You can run the executable jar on every single computer, on one condition - the system have JRE installed.
If you want to, you can also build the .jar using command line, to do that use the following command:
jar cf jar-file input-file(s)
Description from Oracle doc:
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file. The f
option indicates that you want the output to go to a file rather than
to stdout. jar-file is the name that you want the resulting JAR file
to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required. The
input-file(s) argument is a space-separated list of one or more files
that you want to include in your JAR file. The input-file(s) argument
can contain the wildcard * symbol. If any of the "input-files" are
directories, the contents of those directories are added to the JAR
archive recursively. The c and f options can appear in either order,
but there must not be any space between them.
This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest
file for the JAR archive.
After you build your application look for a folder named "dist" in your project's folder. You should find there a file *.jar which can be run anywhere with double click.
STEPS TO FOLLOW:
create a jar
run the jar

How to recompile jar file from command line

I have a jar file that consists of class files and java source files together (actually android unity plugin).
I want change the behaviour of one of the function by modifying the java source code and repackage it to jar file. Is it feasible to do with command line?
Use jar xf <JAR-file> to extract the entire JAR file to whatever directory you're currently on.
Add your new code to the files, removing the old code (make sure you have copies or back everything up, just in case).
Use jar cvf <JAR-file-name> * to create a JAR using all contents in the directory of your files.

Difference between running jar file and exe?

If you have a small program, you can run jar file and it will work fine. But if you convert jar file into exe, you still need java to run your exe file, so what's the difference between them and why do some people convert jar to exe?
An EXE is, ostensibly, an executable program that launches the local java to execute the bundle classes.
As you may know, on your computer you can associate certain file extensions with local programs. For example, .doc files with your word processor.
Similarly, .jar files can be associated with Java, so that Java can execute them. The jar file is considered "stand alone" if it has all of the necessary classes bundled within it, and a proper manifest pointing to the startup class.
So, by associating .jar with Java, clicking on it in your environment will launch Java with the given jar file.
An EXE doesn't need that association. It find java on its own with it's own launcher.
The next step is that you can actually bundle the JRE in to an EXE, so you don't even need to have the user install Java as a pre-requisite. But that's a different process.
People commonly use Java executable wrappers for two reasons - 1. to simply deployment for environments without a JVM, and 2. To make sure the exact Java runtime used for developing the application gets used to run the JAR. However, the practice is not that much widespread.
Java archive or jar is an archive of compiled java byte code and resources which can be run on a java virtual machine. ".exe" is a windows extension for directly executable code mostly used by installers or programs that do not need to be installed. I think your "people" are talking about installers.
An Exe file is an executable file that can be executed in Microsoft OS environment.
Jar file is container of Java Class files, including other resources related to the project. Jar file can be executed only if Java run time environment.
The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file.
The .class files compiled from java files, can not be launched directly. That is why it is needed to be converted to exe before it can run in a windows environment.The usual way to start a java program by batch file is not a convenient way. So inorder to avoid this difficulty we need to convert jar files into exe file.
Also converting it to exe. enables the program to run by simple double click on the program, instead of having to compile it with an IDE or through the JVM.
All that the exe will do is to start a jvm with your app, something like this: "java -jar app.jar".

Java GUI runs executable + Jar Packaging

I am developing a GUI using swing that runs an executable. Currently the executable is being used via Runtime.getRuntime().exec().
I have both the executable and the source code. If I compile my GUI into a jar the executable will not be included into as it currently stands, correct?
I would like the whole thing to run as a single file, is it better then to use the source code or can I package it all as one jar when I'm done some how?
Though I'm writing all the code by hand I do have WindowBuilder for eclipse, I haven't really explored it thoroughly, is there anything in there that might help?
EDIT: Sorry, to clarify: The GUI I want to build uses an executable called src2srcml to take a source file (C, C++, Java) and convert it to an XML File. src2srcml is a separate executable I got from this website: http://www.sdml.info/projects/srcml/
I want to embed this executable into my GUI so that when I compile my GUI into a JAR it contains src2srcml inside it so that I don't need to send a client both my GUI and src2srcml separately.
If you include the executable within the Jar, the executable will not be runnable by the OS.
With that in mind, you have a number of choices...
You Could...
Design a build process that compiles and packages the jar file, takes that and the executable and copies it into an appropriate directory structure for distribution...possible even building installers...
This will mean that you can still access the executable at runtime by using a relative path
This does mean you will have at least two files you will need to distribute, but the over all process is relatively painless...
You Could...
Include the executable within the jar file. At runtime, you would need to extract the executable to the filesystem and execute it.
This means that you will have at least one file you will need to distribute, but it complicates the development process as you will need to find the resources (executable) at run time and extract it to some location before you can execute it
You Could...
Just build an installer for your application. Something like like izPack for example, or what ever installer you want to use.
This means that you can distribute a single file to the client, but when installed, it will unpack all the files into the installation location and you won't need to care...
I'm not sure if I understood your question, but if you want to export you application into jar file and you want it to include all dependecies just have a look at this SO question: How to create a jar with external libraries included in Eclipse?

Building file.jar, External Executable File missing

Assuming that I use NetBeans 7.3 , I created a project that, in a nutshell, receiving as input a set of parameters, it returns as output a print on screen. The project is made up of a number of directories. Each directory contains a class (in file.class form). One of these directory contains an executable in C. I wrote it as the kernel of the Java project.
I built file.jar and I added it as a library in a new project. When I tried to test it, an error message made ​​me realize that the C written program is not was automatically added to file.jar under construction.
One of my first attempt to solve this problem was to manually add the C-executable file. By using the JAR command from the terminal on my Mac, I was able to update the file.jar adding the executable in the right subfolder.
This solution is not served because, moving from project to file.jar, the relative path that leads to the execution of the C-program has changed. So I tried to change this path seeing it from the point of view of file.jar. Yet this attempt was futile.
I defer to those with more experience than me in the packaging and distribution of Java content.
As far as I know, an operating system cannot directly execute an executable that is inside a zip file (which is what a jar file actually is). It has to be first extracted.
So your program could first open its own jar file and extract the executable file into a file on disk, then run that file.
You can create an installer program, to install both the jar file and the executable file to a suitable location on the user's disk.

Categories

Resources