Call methods on class in exectuable jar file? - java

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/

Related

Converting to jar or exe

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?

Creating an executable installer in Java

I am interested in making a one-click installer for my C# application.
I had the the framework of the application down. The logic of the application in the installer() method was:
public static void installer(){
deleteLegacyFiles(); // deletes old files through a find method
moveSQLite(); // moves the database file
if(checkRevit2013()){ // checks whether Revit '13 is installed
movePlugin2013(); // moves my plugin into the Addin folder or Revit
}else if(checkRevit2014()){ // check whether Revit '14 is installed
movePlugin2014(); // moves my plugin into the Addin folder or Revit
}else{
System.out.println("It does not look like you have either Revit 2013 or Revit 2014 installed.");
}
}
However, this Java script (not Javascript, but a Java script) really only took three folders from the /Desktop/ and copies them to their respective target folders. I am interested in a solution that turns all my three folders into one executable file (something like an .exe or .msi) and do the above actions.
Are there any solutions for this for Java? Something that packages multiple folders/files together and then allows for one-click solutions for installation? I'm not exactly how to phrase what I want, as this is my first software development project. Any suggestions are welcome.
You can create a single executable jar file in java. This jar would have an application that does all the copying you've listed above. But instead of copying from the desktop, it would copy directories that are included in the executable jar. A jar is a zipped file type (in fact you can change the extension from jar to zip and examine the contents).
Your strategy will be to create a regular java application, package as an executable jar. Include the directories you want to install as resources in the jar. Check out the jar documentation for all java utility methods and classes to manipulate jars.
http://docs.oracle.com/javase/tutorial/deployment/jar/
Are you looking for making/building an executable jar file? If so you can use something like one-jar.
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
Here are the steps:
Create an executable JAR file with your application's CLASS files in it. (Navigate to bin directory of workspace) Name this "main.jar"
jar cfm main.jar manifest.txt *.class OR [jar cfm main.jar manifest.txt .]
Create three directories: MAIN, LIB, and BOOT
Place your "main.jar" file in the MAIN directory.
Place the jar files that your main application depends on in the LIB directory.
Naigate to Packaging- Create a new JAR file out of the MAIN and LIB directories. Name this one "MyUtil.jar". You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf MyUtil.jar main lib
Extract the contents of the "one-jar-boot.jar" file into the BOOT directory.
Navigate to the BOOT directory, and update the "MyUtil.jar" file with the following:
jar -uvfm ../MyUtil.jar boot-manifest.mf .
Your "MyUtil.jar" file should now be executable. Test it.

Java run jar file & include external jar

Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?

tools.jar dependency

Consider a Java program, launched from a main method, that needs something from tools.jar. In this case, some utility code for connecting to JMX services. Do we have any choice but to wrap it in a shell script that uses -cp to manage the class path? We'd much rather use a MANIFEST.MF classpath.
from http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
the URLs in the Class-Path header are given relative to the URL of the JAR file of the applet or application.
I do not believe you have a choice about using a shell wrapper to get the tools.jar on your classpath. unless you write some custom classloader internally to allow you to find external jars.
If incorporating classes from the dependency jar is an option, I'd go with creation of a "Runnable JAR file". Basically you extract the classes from it and put them with your own classes in the JAR. That eliminates the need for a wrapping script.
To do that in Eclipse, select your project, File -> Export -> Java -> Runnable JAR file; that option will require that you have executed the main class at least once to know what profile to run when you actually run produced JAR.

Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR

What is the difference in eclipse between exporting as a JAR file and exporting as a Runnable JAR file? Aren't they both runnable? What are the pros/cons of each?
The runnable jar contains a MANIFEST.MF file, which defines the Main class to be executed when the jar is run.
Non-runnable jars are just libraries of classes, that can be added to the classpath so that code is reused (it also contains the manifest file, but no main class there)
With the standard JAR file, you have to specify the class with the main method on the command line when running the jar. With a runnable JAR, there is a manifest file that will hold that information so you can just type java -jar myRunnable.jar, or simply double click it.
A runnable jar is a jar file that has an embedded Manifest file that includes the "Main-Class:" declaration. The "Main-Class" must be defined so the java runtime knows which class to call when the jar is "run." If a jar does not include a manifest with the "Main-Class:" it is not considered a "runnable jar" - it is just a library of Java code.
I would guess this is the difference in how Eclipse exports the jar, but not 100% sure.
See this link for more info: http://www.skylit.com/javamethods/faqs/createjar.html
In my case, I used to export as a jar when I had all main class and all the libraries path directory specified in the manifest.mf . If many applications are using same library it is unnecessary to export shared library for each jar. It makes running jar faster. But, many times due to configuration issue in different server class-path cannot access library and in that case it makes sense to export the runnable jar that makes file slow to execute and large.

Categories