I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.
Related
I am taking a course on Coursera and as part of a quiz I am supposed to download their xyz.class and run it. I want to do it in eclipse as all of jar files are added in eclipse and I dont want to modify the class path of system as it is very untidy.
But I dont know how to run a external .class file through?Can anyone help me with that?
Why do you want to run it in Eclipse ?
java classname
souhld do the trick. You can't use Eclipse to run an external class file with a main method without creatin a java project with this class in its class path.
Right-click your Eclipse project, Build Path, Configure Build Path, Libraries, Add (External?) Class Folder.
Just run
java xyz
eclipse might not required.
If you want to really work out through the eclipse then refer How do I include .class files in my project in Eclipse? (Java) answer.
There is another way of approach without using eclipse.
Approach is by creating a executable jar file and by including all required libraries & xyz.class.
Right click on the class file from the Package Explorer -> Run.
goto file >> new project >> Import>> add executable jar files >> select your path where you have stored your .class files>> check include in work place
apply>> ok >>>>>>>>>>>>>>>>>>>>Done
In my java project, i have added some external jars. I want to update a particular jar with my java class file. Please help me how to do this.
Regards
Rajasekaran.G
If the jar you want to add your class to is a 3r party library, I would recommend against doing what you described (it can lead to subtle surprises and unintended side effects).
If the jar you want to update is your own, I would mode the source for the class into the project that generates that jar and it should be added automatically during the generation of the jar.
If you really want to do it by hand, you can unjar the external jar, add your .class file to the resulting directory structure and jar it up again.
You need to update the jar independently try this,
jar -uvf <existingjar> <class file in appropriate folder structure as per package>
e.g.
jar -uvf current.jar NewClass.class
or if your class is in some package say com.my.util, place your class file in com/my/util/NewClass.class and execute the following from prompt
jar -uvf current.jar com
This will update the current.jar file.
If its an external jar, i wonder if you can even do this? If its your own jar, follow the option given by Attila
try to open your .jar file winrar and place your java class wherever you want on the jar.
I have a class called SnmpGet.java in default package. it needs a jar called example.jar. the structure of example.jar is -> degault package-> snmp.class. now SnmpGet.java makes use of this snmp.class. So now it works fine because SnmpGet.java and snmp.class are in default package. But if i move the SnmpGet.java in user defined package like com.test.Work it is not able to detect jar file. what can i do?
I need to use SnmpGet.java in user defined package only. And the jar is the downloaded one. I can not make changes to it.
First, your package does not follow the java naming conventions, no upper cases !
Do you rely on protected methods, classes, attributes...etc of snmp class ? If so, then moving your SnmpGet class to another package will cause you to lose these accesses
You may please try to add the jar(s) dependencies to your classpath. And import the packages & classes from the jar(s) that you would like to use in your SnmpGet.java file. If you are using NetBeans IDE do the following settings to add your jar(s) to your class path:
Right-click on your project folder.
Go to Properties -> Libraries -> Add JAR/Folder.
Add all the jar(s) files to your classpath.
Click on OK to finish the process.
If you are working on Eclipse IDE, you may use the following steps:
Right-click on your project folder.
Go to Properties -> Java Build Path.
Select the Libraries tab -> Add External JARs
Add all the jar(s) files to your classpath.
Click on OK to finish the process.
Hope this resolves your issues.
If you want to call protected methods of snmp.class then your class has to
subclass snmp.class (public class SnmpGet extends snmp) or
be in the same package as snmp.class
Moving the java file from . to directory ./com/test/Work requires to add a package statement to the source file:
package com.test.Work;
Then it has to be compiled (again).
Now, in order to start the application, go back to directory . (the one that contains the com folder now) and do a
java -cp .;example.jar com.test.Work.SnmpGet
(use : instead of ; if you use unix)
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.
I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.