Importing jar files in neabeans - java

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

Related

Java export jar including libraries

I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.

How to use code libraries from github?

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

How do I ignore Eclipse errors if I include junit.jar from somewhere else?

Basically, I created a build.xml for Apache Ant. I use all the .jar files such as easymock.jar, junit.jar, which I downloaded straight from the their websites for my library (set in build.xml).
When I created a new unit test file using those two above .jar files by:
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.*;
Eclipse generated bunch of errors. However, when I ran build.xml as Ant, this didn't seem to be a problem (no compile errors). I guess Eclipse doesn't recognize the external libraries until it actually compiles. How do I ignore these errors for certain unit test files? Or more generally, make Eclipse recognize external libraries?
Look in Project properties->Java Build Path. This has options to add jars to your project.
Simply putting the jar files in the project does not guarantee that it is included in the Project Class path. You need to set it manually.
In Properties-->Java Build Path, add a User Library (for example) and include your jar files there.

Why doesn't an executable jar acknowledge jars inside it?

I made an executable jar that depends on other jars with the command prompt using the format
jar cvfm MyJarName.jar manifest.txt *.class dependentJar1.jar dependentJar2.jar
The jar was made properly and everything seemed fine... But when run, it gets runtime errors because it can't find the class files that my project refers to. Exploring the created jar, the other 2 jars that it depends on are in there just as they should be and they are listed in the manifest.mf class path, but for some reason java is dumb and doesn't actually look inside those jars.
When those 2 jars are in the same directory as the owner jar, java is able to find them and it works fine. But I don't want this; I want those jars to actually be INSIDE it. What's the deal? How can I make an executable jar with other jars inside it work?
You can use something like OneJar or jarjar (or any of several others) to pack up everything inside of a single jar.
If you're building with Maven you could use the Maven Shade Plugin.
You could use a custom classloader like JarClassLoader that will allow precisely what you want.
I don't think you could. Maybe it's somehow possible with customized classloader, but not from-the-box. Use maven shade plugin.
If you use an Ant Builder in Eclipse and put the referenced jars in a library accessible to your project, Eclipse will extract the necessary classes and include them in your distributable jar file.

Netbeans (Java): 3rd library works but how could I compile it into jar?

I'm using 3rd libraries (substance, trident) and I added them as libraries (I added the .jar's) to my project in Netbeans. This works also but if I use the builded jar outside the project directory it doesn't work like it should (the choosen UI don't show), I get the error:
java.lang.ClassNotFoundException:
org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel
I set that UI/LookAndFeel like that in my code:
UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
How could I make this work/run?
You've got 2 choices:
Put the library jar on the
classpath.
Assemble\Build the library jar with
the regular jar.
For option 1, you most likely need the jar located "near" the main jar on the file system; though, this is not necessarily a requirement. When you run the jar you then include the library jar on the classpath.
For option 2, you use some type of tool like maven's assembly plugin or the fatjar plugin in Eclipse (sorry, I don't know what the analog is in NB).
I hope this helps.

Categories

Resources