I understand how to create a JAR file when there is only a single package in a Java project but don't know how to create a JAR file when there are multiple packages in a Java project.
I have a sample project with the following structure:
I have two packages which you can see in the picture i.e.
academy.learnprogramming
academy.ujjwal
I want to create a jar file for the classes which are in the academy.ujjwal package. I tried multiple ways from the Artifacts option but not sure how to do it right.
It's usually uncommon to create jar file from command, Maven, Gradle like build tools are used but you can try following command.
jar cvp filename.jar /path/to/classes
jar cvf learnprogramming.jar src/academy.learnprogramming
jar cvp abc.jar src/academy.ujjwal
I am looking into the github project located at,
SymbolicRegressionInJava. I am familiar compiling java code into jar file but not one huge project using Maven. I looked into Maven and it is another complex system that need to be figured out. The bin folder has already a compiled jar file. I used a command
jar -tf symbolic_regression_1.0.jar
to see the list of classes used for the project. It has both gp and ga directories. I added ga (from another github) folder where is gp folder is at ../src/.. Instead of Maven (which I cannot figure it out even with a tutorials), I attempted manually by typing
jar cvf test1.jar *
It compiled, however, it failed to run like symbolic_regression_1.0.jar. What I am missing here?
I've been used to simply importing jar files into my netbeans projects and then being able to import them in my projects. For example, I included the guava.jar. I saw it under libraries and when I expanded it, I saw things like com.google.common.. So, I just said import com.google.common. in my own classes and I was able to use their methods. Similarly for commons-math3-3.0.jar. But now I similarly included joptimizer-3.2.0-sources.jar (http://www.joptimizer.com/usage.html). When I expand it, I see folders like "src.main.com.joptimizer.functions" However, this time when I go to my projects and try to say import src.main..., it doesn't work they are not recognized. Why did simply importing the jars work before, but not in this case? And what do I need to do in this case to be able to use the methods in the joptimizer jar?
You included sources to you project. Sources means jar with not compiled *.java files.
You should add jar with compiled java classes (*.class files).
Looks like there is no compiled library on http://www.joptimizer.com/usage.html site
and you should compile it manually.
Install maven tool, unpack joptimizer-3.2.0-sources.jar and execute
mvn package
in the root directory (directory with pom.xml)
You will find compiled library in target directory
I downloaded protobuf-2.5.0.tar.gz, extracted it, did the usual ./configure, make, make check, and make install. However, the file protobuf.jar which seems to be needed for using protocol buffers in Java does not seem to be part of what I downloaded. Worse, I cannot find this file anywhere. Please advise me how to proceed.
You can add it as a Maven dependency:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
All dependency information is available at Maven Central, or just download the .jar file directly.
To generate the protobuf.jar file, you need to do what Bruce Martin suggests, but you then need to do the following:
Navigate into the src/main/java/com/google/protobuf directory
Make a directory called bin
Run the following command to build the .java files: $ javac -d bin *.java
Navigate into the bin directory, and finally, run the following command to generate the protobuf.jar file: $ jar cvf protobuf.jar com/
Then, copy and paste the jar to wherever you need it.
You can download ProtoBuf Jar from
Here
then you can put jar inside libs folder
and if want to get Protobuf repository from maven then
add inside pom.xml file.
Hope it will help..
The simple solution is that you should compile the jar by yourself.
Download the src code from https://code.google.com/p/protobuf/downloads/list
Unzip it and follow the instruction in README
Here is what you should do in Linux
./configure
./make
You can install the protoc by run ./make install
Then you need the jar, let me assume you are on ubuntu
install maven first
sudo apt-get install maven
Then follow the instruction in /java/README.txt
If you want the standard jar
mvn package
If you want the lite version
mvn package -P lite
Then you can find the jar in /java/target
I am presuming you are on linux, Try going to the java directory, there is a readme file telling you how to create the jar with / without maven. You will have to create the jar from the supplied sources.
The without maven instructions are:
1) Build the C++ code, or obtain a binary distribution of protoc. If
you install a binary distribution, make sure that it is the same
version as this package. If in doubt, run:
$ protoc --version
If you built the C++ code without installing, the compiler binary
should be located in ../src.
2) Invoke protoc to build DescriptorProtos.java:
$ protoc --java_out=src/main/java -I../src \
../src/google/protobuf/descriptor.proto
3) Compile the code in src/main/java using whatever means you prefer.
4) Install the classes wherever you prefer.
you can download this jar from here
http://mirrors.ibiblio.org/maven2/com/google/protobuf/protobuf-java/2.5.0/
Like all Maven projects, you can get the project jar by clicking "DOWNLOAD (Bundle)" from the project site.
If you look at the URL scheme of the bundle link, you'll notice you can go a directory higher to see all available files for that release, including a sources and javadoc jar.
To create protobuf.jar file using protobuf java follow below steps:
1.Check installed protobuf version:
$protoc --version
2. Install Java and Maven , Check their version:
$mvn --version
$java -version
3. Generate the protobuf.jar file , go to /protobuf/java directory. Run the below commands,
$mvn test
$mvn install
$mvn package
Check /protobuf/java/core/target/ directory,The .jar will be placed in the "target" directory.
You can download google's protobuf.jar from this link
This page serves almost all the jars that you need for development. I downloaded and use it in project
Would you like to download ".jar" file and exclude maven. then
Try it link
"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/2.6.1/"
Should click this link.
"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/"
then you look all version.
I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.