I have a Java protect with the c3p0 JAR. Compiling my project in eclipse works. No errors or something. But when i try to run my application it says the following:
java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources
Do i need to give the jar file the other jarfile from c3p0? Or could i link them when launging the application?
Note: I've understood your problem to be when running outside Eclipse.
The standard Java classloader responsible for loading your classes does not understand jar-files inside jar-files, so you need to do something else:
You can merge all classes from all the jar-files you use into a single jar file. (recommended for you at this point, not general recomendation)
You can put jar-files inside the single jar-files and use a special classloader which understands this.
You can put the referenced jars "next" to the single jar file containing your class files, and have the runnable jar include the necessary MANIFEST.MF voodo needed to refer to them.
The File->Export->Runnable jar option in Eclipse can do all three based on what you choose. Pick the one best suited to how you will get the classes to the final users.
Personally I like the "jars next to the generated jar" as it is the closest to what is supported out of the box by Java, while keeping the original jar files. The simplest is most likely to merge all classes, but when you get more advanced you will find that it has some disadvantages - at this point you will most likely not encounter them.
Add C3P0 as external jar in eclipse
It looks like you did not added c3p0 in the classpath when you run the sample.
However you could use maven and m2e plugin for eclipse and add the following
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
beside the other dependencies you have.
after mvn eclipse: eclipse and you will have your project with a correct classpath.
Related
So I'm a C++ Programmer who is trying to learn Java in order to write a client-side https receptor application, corresponding to a particular website.
My Problem:I found this wonderful-looking Java library online, called "HttpClient" (See https://hc.apache.org/httpclient-3.x/tutorial.html).
Unfortunately, the only way to install the library is manually (download a .zip file with all the .jar files in it). I understand that the include paths are meant to look something like this
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I have no idea how to configure the "CLASSPATH" Environment Variable in order to reflect this (please note that I know HOW to set the variable, just not WHAT to set it to).
My Question:
Could someone please explain to me (in laymen's terms) how to download & setup this library onto my Windows 10 PC such that I can implement the above include statements in my code?
You should install a build/dependency management tool such as maven (there are others). Then you will simply need to add the following to your project pom.xml and maven will handle the rest.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
If you'd like to manually handle adding the .jar (and it's dependencies) you can specify "CLASSPATH=path/to/your.jar" or when running use "java -cp path/to/your.jar
If your project is maven then you can add dependencies in pom.xml as say above
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
If your project is not a maven type then simply download the jar and add this jar file to the lib Folder. Your dependency will automatically resolved. No bug will raised due to dependency.
all you need to do is put the jar in your project's classpath..This can be done in multiple ways depending upon whether you are using any build tools like maven,gradle etc.
But since you are new to java,I suggest you download the jar from here and extract it and put it into any folder which is added to your classpath.
If you have created a webproject then by default WebContent->WEB-INF->lib is already in your calsspath. So just paste the .jar file into this file and you will be good to go.
If you are using maven or gradle or ivy then refer here: http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.2
N.B.:you don't need to add anything to your windows classpath or modify any path variables of your OS. these are all project specific classpaths. So do it your project. Use a build tool like maven if you wish to access these more easily. Also recommended is to use eclipse or netbeans or some other mordern IDE
Just make sure that each of the .jar files it comes with are in your CLASSPATH.
e.g.
export MY_INSTALL_DIR=/some_dir/httpcomponents-client-4.5.2/lib
export CLASSPATH=$MY_INSTALL_DIR/commons-codec-1.9.jar:$MY_INSTALL_DIR/commons-logging-1.2.jar
and so on... I did the first two CLASSPATH elements above, and you ultimately need one element for each of 10 or so JARs I see in the dist.
Think of the .jar files as something like .so or .dll files - the compiled binaries. Think of this as the equivalent of putting .so or .dll files in your PATH.
In my example, I set the CLASSPATH via environment properties, like a .bashrc file or the like. Since you're on Windows, you would use the Windows environment variables available via the Advanced System Settings control panel. You can also set them via the command line (javac -classpath and java -classpath), via your build tools, or other ways. I found a couple of helpful links that discuss this: this one and this other one.
Long-term, you should learn how to use Maven, like the other answers here suggest, but first get to grips w/ Java basics. Just so you know, Maven is something like nmake from your world, plus the ability to automatically download and manage 3rd party dependencies, plus a whole lot of other features.
I have a project in Scala (a kind of test utility) which is currently used only in sbt run way. However for certain demo I want to prepare it in a form which does not require sbt or scala preinstalled (only JVM).
First I've tried to use sbt-assembly plugin but soon get lost fighting with duplicate entries. So now I'm curious whether I can simply compile it to:
single jar-file containing application itself;
and lib directory containing raw set of dependency jars.
I hope that in such case it would be easy to run with the help of Main-Class and Class-Path: ./lib/* fields in the manifest - am I wrong? If this is correct, how can I achieve this?
Update: at last I conquered (it seems so) the sbt-assembly approach, so now the question is not as urgent (though I'm still curious to extend my knowledge of using sbt).
When execute sbt-assembly, all depedencies, App and resources will package into a single jar file.
You can override config properties in runtime by:
java -cp conf/:myAppDemo.jar App.run.mainClass
put your config properties files in conf folder.
Sbt one jar plugin can resolve more dependency conflicts, then assembly plugin.
Also take a look on merge section of assembly plugin, that can help you to fix problems like log4j.xml duplication. If you have problems with two classes with the same classpath having different content, try to exclude some duplicated dependencies (library management)
How do I prevent IntelliJ from including 3rd-party JARs inside my JAR ?
It's insanely annoying. Basically I want one of these:
to produce jar with my code only. 3rd-party jar will be referenced in manifest.
OR
to produce jar with extracted classes of 3rd-party libraries.
The only problem is that IntelliJ produces JAR with other jar files inside. They are useless because java doesn't see them anyway when I run my jar via java -jar my.jar. I have to manually delete them and repack JAR/ZIP file.
Dependencies are not marked as "Export" in Settings.
I think jars end up in my jar because I added "compile output" in layout of my artifact. But I'm not sure how I can make compile output without jars of dependencies.
I tried setting Scope of dependencies to "Provided". It didn't help. They still get copied to output.
Thanks!
This behavior is controlled by the following options when you create a jar artifact in IDEA:
Refer to help for details.
When I clean and build, Netbeans generates a .jar file for my Java project, and it uses a "lib" folder which has all of my external Jar files that I use. Can I somehow tell Netbeans to package these Jar files into the .jar it makes? So that I can run my project as a stand-alone .jar file?
I am using Netbeans 7.1.1 on Mac OSX
The short answer is no, the long answer is complicated.
Firstly, Java does not support embedded Jars (ie, you cann't simply add the depended Jars into the main Jar).
Secondly, uncompressing all the Jars and merging them into one will overwrite any resources/classes that share the same path/name. This may not be an issue in small projects but is a major issue in large ones (we have this problem with the project I'm working on at work).
One solution is to use something like One-Jar, which basically uses it's own boot class loader to all you to reference Jars inbedded within a single Jar.
I've had issues with this when using our own custom classloader, but otherwise it seems to work well.
It includes Ant support, so you can include it in your projects Ant build script, if you not using Maven, otherwise, you'll need to devise your own build process
There is no options in netbeans to do that.
There are some other options that a quick search would help, but requires manual intervention.
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.