0

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.

mstaal
  • 590
  • 9
  • 25
  • Possible duplicate of [Java creating .jar file](https://stackoverflow.com/questions/4597866/java-creating-jar-file) –  Jul 29 '19 at 13:12
  • If you're using, Gradle, then it should already do this for you (check the `build/libs` folder of your project). If it doesn't, then post your `build.gradle` file and which commands you use to build. – Mark Rotteveel Jul 29 '19 at 15:20

2 Answers2

1

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.

-1

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.

  • Could you give an example? What is a class file, and does that task I am asking for have a specific name? And what about the issue concerning the resource files I would like to include? – mstaal Jul 29 '19 at 13:12
  • public class Simple { public static int add(int a, int b){ return a+b; } } you can simply create a java file add some functions above then you can generate the jar file either command line or using eclipse you can export java to jar. Then create another java project there add the jar you created then you can make use of the functions in that jar – Ajith Naruto Jul 29 '19 at 13:25