I am using Netbeans IDE for a java project. In this project i need a jar file "htmlunit-2.6.jar".
I have included this jar file in the project libraries folder. I have instantiated one of its class "WebClient" but this class needs other classes of "commons-httpclient-3.1.jar" file.
Now I have also included "commons-httpclient-3.1.jar" file in the project libraries folder. But when I compiled my source file, it throws
ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
Kindly tell me how to handle this situation when one class in one jar file needs other classes in other jar file.
Simply put the required jar files on the classpath at compile-time and it should work. If you were doing it from the command-line then it would look like this:
javac -cp jar1:jar2 my.Application
If you are using NetBeans then you need to tell NetBeans that both of the JARs are on your classpath. It will be definable in a Project > Properties wizard as described here and also here from the tutorial
The ClassNotFoundException tells you that your libraries have some dependencies that you don't have included in your classpath at runtime. Your source is OK, because if you have used something not available, NB will tell you this at compile time (or before when editing).
So, welcome in the "dependency hell" of Java. For small projects you will be able to check all dependencies by hand with readme files, docs, etc and put them in the project config as oxbow_lakes said. For bigger things look at maven. It will do (most) everything for you !
(Maven is available in NB6)
Related
I do have a project that depends on external libraries (jfoenix is one of them). I didn't want to add the libraries using IDEs like Eclipse, Netbeans or any other IDE. The Directory structure looks like,
Project Folder:MyProject
Package :afa
Where and how should I include the external jar files and compile my classes which are inside the package? I found answers that say, put your jar files in the classpath or -classpath but I need a step by step guide on how I can do this. All my java classes are in the same package.
I'm currently trying to compile my project which would include two external libraries.
json-simple.jar and mysql-connector-java-5.1.42-bin.jar
At the moment I compiling my program and the problem is that i need to include these libraries in the classpath of created compiled project, but i do need only that these libraries would inside .jar project compiled file and i can simply run .jar file and make it work withuot including classpaths or something like that.
Is that possible, don't have idea how.
Yes, it is possible.
I'll help you a bit:
How to make an executable jar file?
Basically unpack the files from the libraries into the folder with your compiled classes (you know how to compile them, right?) then prepare a MANIFEST.MF, put it the folder with all these classes, then create a jar as explained in the answers under the link (jar cfm jarexample.jar jexample.mf *.class). If you have any problems then read the documentation of the commands like jar and javac.
Have fun!
I have to write a java application which I'm putting together using eclipse and it relies on open source code. This application needs to be self-contained, meaning that I'm supposed to create a jar file that has no external dependencies.
I can use the open source code when I reference the jar files in the project's build path, but the idea is to have the actual source code as part of the eclipse project, side-by-side with my code.
The source code can be found here: http://hc.apache.org/, but when I import an existing file system into my project I can't quite get things to work. The packages end up with the wrong names, breaking references, and I can't do anything. Notice that the folder containing the source code has this structure:
httpcomponents-client-4.2.3\
src\
httpmime\
httpclient-osgi
httpclient-contrib
httpclient-cache
httpclient-benchmark
httpclient
fluent-hc
each of those subfolders has src/main/java/org/apache subfolders.
Can someone please explain how to do this? Am I supposed to import everything one java file at a time?
Use a tool like OneJar, FatJar, JarJar, etc. to create a single-jar application.
As Charlie mentioned, the Maven Shade plugin is another choice, particularly if you're already using Maven. If you're not, consider it or another transitive dependency management tool.
Some tool should be used, IMO, and it's more important the more dependencies you have.
Alternatively you could use a jar class loader and include the jar file in your artifact.
I would most definitely not include the source of dependencies in your own project.
I am creating an executable jar file for my program in java.
The program uses jtds.1.2.jar and javacsv-2.0.jar.
Is it possible to include the external jars when I create my executable jar file??
Please help.
Thank you.
Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
Yes it is possible.
You may use eclipse export jar function doing this
Sure it's possible! Just add the classes of the Jars to your Jar!
You can either do this by hand (unsuggested), or any build tools like Ant ( target) and Maven (search for shading plugin) can do that for you.
If you don't want to bother creating any scripts, just use the IDE to do it for you (newer Eclipse versions have a checkbox for including libraries automatically, for older ones just install a plugin that does the job for you automatically, like the FatJar plugin).
It's possible to create a folder structure like this
main directory:
your_app.jar
subdirectory:
other jar files
Then add the dependencies to your jarfile's manifest and "zip" the whole thing together, to move it to the computer where you want it to be. Netbeans, for example, supports this kind of deployment.
For a more general / "foolproof" solution, you will have to create a setup program.
I have a project that shares models with my android project. I have a separate eclipse project just for models and other shared code. I add this to my play project as a dependency in eclipse.
In eclipse, play compiles and starts without problem. However, I went to deploy to GAE and found that the compilation stage of play's packaging fails because it can't find the models.
I suspect I could hack the ant build files, but that seems brittle.
Is there a standard way to add extra directories to the play compilation source tree or the classpath?
Make a jar-file with your classes and put it in /lib. That's where I put my libraries.
Files in the application /lib folder is automatically added to the class path by Play Framework. See Classpath settings
From Anatomy: "The app directory contains all executable artifacts: Java and Scala source code, templates and compiled assets’ sources" ... and further: "You can of course add your own packages, for example an app/utils package"
You can copy java source files to make a hierarchy of packages under /app, e.g.:
/app/sharedcode/project2/models/domain1 and import that in WhateverController.scala as:
import sharedcode.project2.models.domain1._