How to use code libraries from github? - java

I need some tools to read JSON from a URL. However, when I tried to use the JSONObject class, it was unavailable. I looked it up and it was suggested that I should download the required library : https://github.com/douglascrockford/JSON-java
I'm unfamiliar with downloading and creating a jar of Github repositories. I have downloaded the repository in zip fromat. ( I am using windows 8). I navigated to the location, extracted the files and tried to create a jar.
I found half of the solution in : How to make this (github) source into a library/jar?
Based on the Suggestion i used the command to make jar:
git clone git://github.com/douglascrockford/JSON-java
cd JSON-java
jar cf JSON-java.jar *.java
I got a jar generated in the folder, I dragged it over to the library folder in my eclipse project.
However, I still don't find any of the required classes available while coding.
Additionally, I tried compiling the code >javac *.java and then tried the jar creation step but no avail.
What steps I missed?

When you have to attach external library in Java, generally you should have jar with compiled sources.
What we have in repository is source code, nothing more, so you can't just pack it and get compiled files (*.class).
So, your options here are at least so:
Download compiled jar, provided by author and attach it to the project within Eclipse (How to import a jar in Eclipse)
Use maven project, for which you can define dependency on some external project: http://mvnrepository.com/artifact/org.json/json/20140107
Compile the project yourself (not recommended): in Frederic Henri's answer

not sure what you mean with the compilation of java file and "no avail"
but you need to compile java files before you package them as jar :
mkdir build
javac -d build/ *.java
then package the build directory as jar
jar cf JSON-java.jar -C build/ .
then import the jar file in your library of the project - make sure to add it in your eclipse lib settings and you should be able to reference the org.json package

Related

Create JAR file of a specific package in a multi-package project

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

Compiling Java Project into Jar file by from command line instead using Maven

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?

Importing jar files in neabeans

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

Where to find protobuf.jar for using Google protocol buffers in Java?

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.

How to build a distributable jar with Ant for a java project having external jar dependencies

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.

Categories

Resources