I want to include a jar file which is used by only 1 package in my project.
|____package1
|____package2
|------------class1
|------------class2
|------------newjar
This is since I am trying to run Unit tests that needs a specific classes contained in this jar.
I do not want this jar for other packages, since I have edited this jar and created another jar for the rest of my project.
Not sure if this is allowed. Found a few articles that speak about including the jar in runtime but that is not what I want, alteast thats what I feel.
If you have the ability to edit the jar already, just rename one and include them both. There is no way (that I know of) to add a jar to the build path of only a single package.
If you are creating the jar from your own project then while creating the jar use the package/classes you want to be available in the jar.
If you are not creating the jar from your own project, let say you are using ODBC jar. In java build path, we include the complete jar which will be vailable in the classpath. So that we can import the resources when required. Hope this answer your questions.
Related
I have read numerous posts regarding this, and I was still not able to find a clear-cut answer.
We have the need to use a proprietary SDK in our maven project and this SDK contains ~315 jar files that are needed for around 30 lines of code (SAP product). Every answer I read dealt with adding individual jars to your local maven repo. That is fine and I understand that, but is it possible to add an entire directory of libraries. These libraries are only needed for compiling the project since they are already on the classpath of the target server (They would all be scoped as provided in a pom).
I've tagged Netbeans 8 since that is the IDE I am using, so if anyone knows a hack to get a maven project in netbeans compiled using libraries on Netbeans classpath that would be a good solution as well...
JAR's are just java .class organized in folders and Zipped. Extract all those 315 JARs to somewhere, thus merging all of their content, and then Zip it again to one single fat JAR file. Add this fat JAR to your local repository as you have read elsewhere.
This other question can help you with the JAR merging thing: How to combine two Jar files
Although there are many messy workarounds for this, the ideal would be to let the compilation fail and search for the missing compile jars using a search utility like agent ransack you can search within the jars in that directory for the missing classes referenced in the compiler errors. As you find the jars you need, add them as dependencies with the scope of provided.
A less clean option would be to zip all of the jars, use the dependency plugin to unpack them to a folder and add that folder to the classpath of the build, then remove them or exclude them from the final package.
How do I include all the jars into Launch4J so that I can build an exe without any dependencies?
Thanks.
I think the trick here is to package all of your class files and all dependencies within a single jar. This means extracting the contents of those dependent jars, and then repackaging them.
The answer to this question appears to be related and might help: Launch4J - how to attach dependent jars to generated exe
I think an entirely manual process is also possible. Compile your class file into a build directory. Open each dependency (jar file) as if it were a plain ZIP, and extract the contents into the directory with your class files. Repeat this for every jar. Now your code and the dependencies sit side by side - package the entire set of files into a single JAR and proceed with the steps needed to package that up with Launch4J.
I added external jar file(stdlib.jar on picture) to my project in IntelliJ(12.1.4), but when I want to use some static methods from it in created package(ChapterOne) it can't resolve it. However it finds classes in default package without problem. How I can make it work ?
It seems that dependencies are ok.
The libraries were configured correctly in IDEA, but the jars actually contained sources, not classes. So the solution is to unpack these sources from the jar files into the module src directory.
So I'm fairly new to Java and especially Eclipse, so please excuse my ignorance. I took a project from a server and copied it locally to my machine. When I opened the workspace, I had many errors due to it not being able to find the jars. This makes sense because I don't have the same dir structure as the server I copied from. So if I copy the same external jar's to my machine and get it to compile into a jar and copy it back to the server, will it work? Or will it fail because now the external jar's are in a different place than it is expecting?
Also, down the road should I put the external jars into regular jars to avoid this problem?
You should be OK. Java is using what is called classpath to locate dependencies. The classpath may be different on the development machines, but as long as all the dependencies are on the classpath in the production everything should work.
To avoid issues with the synchronisation of directory structures the most common way is to use Maven - it will manage all the dependencies for you (but you have to manage the pom.xml - the Maven's project descriptor). A little clumsier way is to have the dependencies in the project, however you may end up with many projects having to include same jars, and then there will be version conflicts and so on.
For small projects you can manage dependencies yourself, however larger projects will need a more thought through strategy (like Maven).
In regard to the executable jars, make sure the Class-Path entry in <jarfile>:\META-INF\MANIFEST.MF is correct, e.g. where it references other jars, those jars are going to be there in the production. For example, assume we have ourjar.jar and assume this is a snippet from its MANIFEST.MF:
Class-Path: lib/myteamjar.jar
It will then be expected that a following directory structure is in place:
lib/myteamjar.jar
ourjar.jar
No, the location of the external jars does not mater. What you want to do is put the external jars on your classpath. How you do it depends on how you are running your java code. If you are running it from the CLI using the java command, it takes the classpath as an argument. If you want your code to build/run in Eclipse, you need to right click on your project, select "Build Path" > "Configure Build Path..." Use the "Add JARs..." button to add jars that are part of a project you have open and "Add External JARs..." to add jars that reside outside of the project. See specific documentation for your tool for more details about classpaths.
I would not recommend Maven to somebody who is fairly new to Java and Eclipse. I would forget about Eclipse, too.
You have a packaging and CLASSPATH issue. Focus on that.
What kind of project are you talking about? The answer you get will depend on what type of app you're creating. Is it an executable JAR? Then the right way to do it is to package everything into a ZIP file that's laid out exactly as the CLASSPATH in the JAR manifest expects.
If it's a web app, the right thing is a WAR file, with all the JARs your app needs in the WEB-INF/lib directory.
If you package things properly, you should end up with a single package that has everything laid out the right way. You should be able to deploy it to the server and make it all work.
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)