I have around 10 java files with their corresponding class files. The whole application works when i execute one of the main class. I execute it using cmd. I need help converting it into a single self executing file. eg: exe or jar
A jar file is little more than just a zip of all classes and resource files. You need to add details into META-INF/MANIFEST.MF file so that the class containing the main method to be executed is know.
This should help: Link
You can try invoking the jar file as follows:
java -cp <jarfile.jar> <Complete.Package.ClassNameWithMainMethod>
You can make jar by Exporting the project using following methods in Eclipse IDE
File->Export->Java->JAR.
Note: This is already have a solution to change to exe or JAR.
How can I convert a .jar to an .exe?
Related
I've created a method in a Java class that does some encryption. This class utilizes methods from some external jar files. The requestor of this method has asked that I include my class and the external jar files in one jar file. I am using Eclipse for my IDE. As I understand it, I need to create an executable jar file in Eclipse, in order to package the external jar files in my own jar file. In my project, I also created a basic class with a main method in my project that simply calls my method mentioned above (to provide a "main class" for the run configuration for the executable jar). I exported my project to an executable jar file, and verified that it runs (java -jar jar-file-name) in a command prompt window. I created a second test project in Eclipse, and added my executable jar file to the build path. I created a simple class in this test project with a main method that calls the method mentioned above (on the class in my executable jar file). This compiled, so I exported this project to a normal jar file, and tried to run it (with java -classpath executable-jar-file-mentioned-above;test-project-jar-file class-name. This fails with a NoClassDefFoundError, referencing a class found in one of the external jar files.
What am I missing here? This is my first experience with an executable jar file. Is there anything special that needs to be done when calling methods on a class in an executable jar file?
There is nothing special about executable jar files. The only difference is that an executable jar file defines a main class (with a main() method) that will be started when you run java -jar ....
A jar file is in fact a plain zip file containing classes, resources and some additional meta-information.
When you run your jar file and get a ClassNotFoundException, this is an issue with you class path. So you are somehow missing classes, you depend on, located in your external lib.
There are two ways to fix your issue:
Add the library jar to the class path when you run your runnable jar
Repackage your executable jar to contain all the classes from the external library
I don't know what the Eclipse export functions do. But I would recommend taking a look at the exported jar file first using your favorite zip utility.
I would also recommend using a build tool like maven to create your jar. Here is an example how this can be done using the maven assembly plugin: https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
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).
Sorry if this is a little confusing. I have a java file that uses a compilation command with another jar that I want to make into an executable(not .exe). I followed an online example to make a regular jar but get the NoClassDefFoundError due to the jar that it needs to run not being included. I am not sure what I either need to add to my manifest or the jar creating command to achieve this.
Thanks
I have not written java in a long time but try this,
Export(right click and select it from options) the project you're working on
Select runnable jar file.
Decide what you want to do with libraries
Finish.
NB: Try having the class files in the same location as the jar
I have services.jar file and have few packages inside this jar file. I need to modify the code of one its java classes.
Example - services.jar
Packages inside services.jar - search , util
Classes inside search package - Filter.java - Need to edit this code and need to make a new jar file.
Please guide me.
You can do so if you have the code available with you that you want to modify. If yes, then you have to extract the jar either from command line or using any tool such as winrar. Here is the command to extract the jar from command line:
jar -xvf services.jar
Replace the modified or updated /class file in the extracted directory and recreate the jar using the following command from commandline:
jar -cvf services.jar
You should not be updating the java file. Jar file is for archiving the compiled java classes
Instead you should be updating the class file after making the changes
Refer this article
If you have to, you can shadow the jar file with another placed ahead of it on the class path. However, if you do not understand what you are doing, and you miss some dependencies, you are likely to get a NoClassDefFoundError.
If you need to modify part of a class's behavior, you could weave in an aspect with AspectJ. You might use an Around aspect if you want to replace the behavior.
I have updated my ant build.xml file to include a new file and a new folder. After creating the .jar I check if they exist in the jar by 'unzip\extract', and they are there.
But when executing the .jar neither the folder or the file gets extracted.
Am I missing a step?
Look into getResourceAsStream. It'll keep you from having to extract the files from the jar file. Unless that's your goal.
Your application should be able to use the file directly from within the jar, no need for extracting it. Or do you mean something else?
Are you doing something specific to extract the jar file? I ask because normally jar files are not extracted when executing them.
If you run "java -jar myJar.jar" or "java -cp myJar.jar com.example.MyMainClass" the jar files that is referenced will not be extracted. Java will load your classes and resources directly from the jar file without extracting it.
If you wrap your application up using One-JAR, you can specify an attribute in the Manifest file to extract files that you want (See the One-Jar-Expand manifest attribute).
As a bonus, you will also be able to wrap any dependent libraries along with your code, creating a single distributable jar.