Link to home
Start Free TrialLog in
Avatar of bilgehanyildirim
bilgehanyildirim

asked on

IntelliJ IDEA and external JAR libraries

Hi,
I am trying to develope an application using IntelliJ IDEA 8. I decided to you javamail library which I downloaded from
http://java.sun.com/products/javamail/downloads/index.html

what I am trying to do is create one jar file which will contain everything that program needs and when I run
java -jar program.jar
it wont give me
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingException
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

is it possible?
Thanks
B
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image


If you want to have a single jar file, you can uzip all libraries you are using and put them into this one jar.
Avatar of bilgehanyildirim
bilgehanyildirim

ASKER

It didn't work like that.
I just tested something.
When I clean & build same project using Netbeans 7, netbeans creates the jar file and also a lib directory where all external lib jars are and it works perfectly fine.

is is possible to do this on intellij?
Any ideas?

I usualy use ant for build, that way it doesnt matter which IDE I use for development.

Its not very complicated to create a deployment file with ant. Check it out:

http://ant.apache.org/
Hi there,
Ant is too complicated for me. I am very new with this Java thing.
I'd really appreciate if someone could go through step by step where I can build my java application with all external libs and copy it to the destination pc (centos in this case) and run without coming across with class not found exception.

When I run my project within IntelliJ, it works perfectly fine. But when I create the jar and run on the remote server using java -jar HelloWorld.jar I get following error message.

Can someone please help me?
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: javax/mail/NoSuchProviderException
        at com.netetechs.thread.EmailThread.run(EmailThread.java:27)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.NoSuchProviderException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Open in new window

nothing?

When you run it in IDEA, you can see the command line that runs the application (it is written the bottom window). You can check that command line to see which classes are you using, or you can copy it here for us to see it.

The important part is -cp, where all the classes are stated.
It is
This means it's loading these jar files everytime I run my appalication, doesn't it?
What if those jar file are not present on the target machine? I assume some of them are standard JVM jar files so they should be there, but what about the ones I installed later on, such as mail.jar? Is it possible to put them in the jar file of the application so that I don't need to worry if they are present or not on the target machine?

Thanks
Bill
 -classpath "C:\Program Files\Java\jdk1.6.0_12\jre\lib\charsets.jar;
		     C:\Program Files\Java\jdk1.6.0_12\jre\lib\deploy.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\javaws.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\jce.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\jsse.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\management-agent.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\plugin.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\resources.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\rt.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\aspectjrt.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\dnsns.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\jackson-core-asl-0.9.7.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\jackson-mapper-asl-0.9.7.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\java_memcached-release_2.0.1.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\localedata.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\mysql-connector-java-5.1.7-bin.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\sunjce_provider.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\sunmscapi.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\sunpkcs11.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\activation.jar;
			 C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\mail.jar;
			 C:\Documents and Settings\Administrator\IdeaProjects\GlobalTick\out\production\GlobalTick;
			 C:\Program Files\JetBrains\IntelliJ IDEA 8.1\lib\idea_rt.jar"

Open in new window


I guess the mail.jar is the one that is missing. Try running your application by adding the mail.jar to the classpath.

You have to have all the jars, of course some are standard and you dont have to add them. But you have jdk installed on your computer, others may only have jre (so some jars may be missing).

Its best to try running your application localy with jre and add the libraries you need (obviously mail.jar would be the first here).
Ok, I have run it on the centos server via
java -jar -classpath "/opt/SDK/jdk/jre/lib/ext/mail.jar" GlobalTick.jar
and I still get
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: javax/mail/NoSuchProviderException

First try running it locally on your computer, so you eliminate all other variables.

If the application runs normally when started from IDEA then it will work anywhere, its just the matter of setting classpath.

You can check where the missing class is inside IDEA by typing: Ctrl+N and then typing the name of the class, in your case NoSuchProviderException, then just press F1 and select view in project.
It runs fine within the IDEA and it runs from Centos server if I run the following commands. But when app needs to send mail, I receive that error message and "NoSuchProviderException" is in javax.mail package which is in mail.jar and it's in my classpath.

App also uses MySQL and it wors perfectly fine with this setup.
java -jar -classpath "/opt/SDK/jdk/jre/lib/charsets.jar:/opt/SDK/jdk/jre/lib/deploy.jar:/opt/SDK/jdk/jre/lib/javaws.jar:/opt/SDK/jdk/jre/lib/jce.jar:/opt/SDK/jdk/jre/lib/jsse.jar:/opt/SDK/jdk/jre/lib/management-agent.jar:/opt/SDK/jdk/jre/lib/plugin.jar:/opt/SDK/jdk/jre/lib/resources.jar:/opt/SDK/jdk/jre/lib/rt.jar:/opt/SDK/jdk/jre/lib/ext/aspectjrt.jar:/opt/SDK/jdk/jre/lib/ext/dnsns.jar:/opt/SDK/jdk/jre/lib/ext/jackson-core-asl-0.9.7.jar:/opt/SDK/jdk/jre/lib/ext/jackson-mapper-asl-0.9.7.jar:/opt/SDK/jdk/jre/lib/ext/java_memcached-release_2.0.1.jar:/opt/SDK/jdk/jre/lib/ext/localedata.jar:/opt/SDK/jdk/jre/lib/ext/mysql-connector-java-5.1.7-bin.jar:/opt/SDK/jdk/jre/lib/ext/sunjce_provider.jar:/opt/SDK/jdk/jre/lib/ext/sunmscapi.jar:/opt/SDK/jdk/jre/lib/ext/sunpkcs11.jar:/opt/SDK/jdk/jre/lib/ext/activation.jar:/opt/SDK/jdk/jre/lib/ext/mail.jar" GlobalTick.jar

Open in new window


Can you send mail from within IDEA?
Yes, I can.
Have i lost your attention :)

Well, I'd try the following:

1. Try running the application locally from your computer (not from IDEA).
2. This should work for sure, if no other way then the command line from IDEA will work (but eliminate the end of the command line where your class is stated as a parameter and an IDEA class actually runs the application)
3. Trim down the command line, so that only the actually needed classes are in the classpath (remove them and try)
4. Once you have the minimum, you know what you need on the other computer
Ok, did what you said. Here are the two commands I used.
Development one works perfectly fine, but I get exception (java.lang.NoClassDefFoundError: javax/mail/NoSuchProviderException) on the centos server
windows (development) : "C:\Program Files\Java\jdk1.6.0_12\bin\java" -jar -classpath "C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\mysql-connector-java-5.1.7-bin.jar;C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\activation.jar;C:\Program Files\Java\jdk1.6.0_12\jre\lib\ext\mail.jar" GlobalTick.jar
 
Centos (production/staging) : java -jar -classpath "/opt/SDK/jdk/jre/lib/ext/mysql-connector-java-5.1.7-bin.jar:/opt/SDK/jdk/jre/lib/ext/activation.jar:/opt/SDK/jdk/jre/lib/ext/mail.jar" GlobalTick.jar

Open in new window


Ok. This is exactly what I would've done.

What version of Java do you have on Centos? Type java -version.
on Centos I have
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)

on windows i have

java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

Hm. Out of ideas, I have to admit.
:) thanks for time anyway :)
You are going to laugh!!! I found the problem.
Actually i have two java installed. one is under
/opt/SDK
which is for glassfish
and one is under usr\java\jre1.6.0_12

:)
So when I copied jar files under lib/ext directory even without classpath I can run the app successfully
java -jar GlobalTick.jar

:))))

ok, i have one last question for you
is it possible to copy these jar files under a lib folder where the app jar file lives and use them?
I created a dir called lib and copied them here. So it is
GlobalTick.jar
lib
|
-- mail.jar
|
-- activation.jar
|
-- mysql.jar

and I tried to run it like
java -jar -classpath "lib/mysql.jar:lib/mail.jar:lib/activation.jar" GlobalTick.jar but I get class not found exception (for mysql)
I also tried
java -jar -classpath "/usr/local/globaltick/lib/msql.jar............

same error

can you help with this?

Glad you resolved the problem.

Yes, you can put the libraries you need where you want and then define them in the classpath.

Could it be that you are not typing the correct names of the jars? I mean, you had mysql-connector-java-5.1.7-bin.jar and here you have mysql.jar and then you have msql.jar. Check the names and the path of all jars.

:) no i use the correct names, but to make it short here I just typed mysql.jar
This is exactly how i run it

java -jar -classpath "/var/www/vhosts/xxxx/application/external/GlobalTick/lib/mysql-connector-java-5.1.7-bin.jar" GlobalTick.jar

Open in new window


Try mistyping the jar file name on purpose, just to see if the system will tell you that it doesnt find the file.
If i put foo.jar instead of mysql jar i get the exact same error message
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
it's as if it's ignoring the classpath
I also remove the mysql... jar from my jdk lib folder.
so there is only one mysql.... jar file in the system and it's under lib folder of my application dir

Do you run this in exactly the same way on windows? I mean, deleting the files from lib/ext and putting them in your folder.
Same error
C:\Documents and Settings\Administrator\Desktop\New Folder>java -jar -classpath
"C:\Documents and Settings\Administrator\Desktop\New Folder\lib\mysql-connector-
java-5.1.7-bin.jar" GlobalTick.jar

Open in new window


It does look like your classpath is not evaluated. Try running it without the -jar. Just: java -classpath and add the GlobalTick.jar to the classpath and state the full name of your main class at the end.
on windows this works fine
C:\Documents and Settings\Administrator\Desktop\New Folder>java -classpath "lib\mysql-connector-java-5.1.7-bin.jar;lib\mail.jar;C:\Documents
 and Settings\Administrator\Desktop\New Folder\GlobalTick.jar" globaltick/Main

and on Centos this works fine as well

java -classpath "lib/mysql-connector-java-5.1.7-bin.jar:lib/mail.jar:lib/activation.jar:GlobalTick.jar" globaltick.Main

Well, then the problem is solved?

Or do you want to run it with -jar option for some reason?

Cause in that case I'd try just changin the  order of -jar and -classpath options.
I ran
java -classpath "lib/mysql-connector-java-5.1.7-bin.jar" -jar GlobalTick.jar

and got java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

why is this classpath so complicated!

Why do you want to run it like this and not with the command line that works?
I can use that, but i need to know why the jar method doesn't work..:)
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial