[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7254
  • Last Modified:

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
0
bilgehanyildirim
Asked:
bilgehanyildirim
  • 20
  • 18
1 Solution
 
Dejan PažinCommented:

If you want to have a single jar file, you can uzip all libraries you are using and put them into this one jar.
0
 
bilgehanyildirimAuthor Commented:
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?
0
 
bilgehanyildirimAuthor Commented:
Any ideas?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Dejan PažinCommented:

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/
0
 
bilgehanyildirimAuthor Commented:
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

0
 
bilgehanyildirimAuthor Commented:
nothing?
0
 
Dejan PažinCommented:

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.
0
 
bilgehanyildirimAuthor Commented:
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

0
 
Dejan PažinCommented:

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).
0
 
bilgehanyildirimAuthor Commented:
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
0
 
Dejan PažinCommented:

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.
0
 
bilgehanyildirimAuthor Commented:
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

0
 
Dejan PažinCommented:

Can you send mail from within IDEA?
0
 
bilgehanyildirimAuthor Commented:
Yes, I can.
0
 
bilgehanyildirimAuthor Commented:
Have i lost your attention :)
0
 
Dejan PažinCommented:

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
0
 
bilgehanyildirimAuthor Commented:
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

0
 
Dejan PažinCommented:

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

What version of Java do you have on Centos? Type java -version.
0
 
bilgehanyildirimAuthor Commented:
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)
0
 
Dejan PažinCommented:

Hm. Out of ideas, I have to admit.
0
 
bilgehanyildirimAuthor Commented:
:) thanks for time anyway :)
0
 
bilgehanyildirimAuthor Commented:
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?
0
 
Dejan PažinCommented:

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.

0
 
bilgehanyildirimAuthor Commented:
:) 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

0
 
Dejan PažinCommented:

Try mistyping the jar file name on purpose, just to see if the system will tell you that it doesnt find the file.
0
 
bilgehanyildirimAuthor Commented:
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
0
 
bilgehanyildirimAuthor Commented:
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
0
 
Dejan PažinCommented:

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.
0
 
bilgehanyildirimAuthor Commented:
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

0
 
Dejan PažinCommented:

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.
0
 
bilgehanyildirimAuthor Commented:
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
0
 
Dejan PažinCommented:

Well, then the problem is solved?
0
 
Dejan PažinCommented:

Or do you want to run it with -jar option for some reason?
0
 
Dejan PažinCommented:

Cause in that case I'd try just changin the  order of -jar and -classpath options.
0
 
bilgehanyildirimAuthor Commented:
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!
0
 
Dejan PažinCommented:

Why do you want to run it like this and not with the command line that works?
0
 
bilgehanyildirimAuthor Commented:
I can use that, but i need to know why the jar method doesn't work..:)
0
 
Dejan PažinCommented:

In that case, again try it locally first. On windows from the C:\Documents and Settings\Administrator\Desktop\New Folder run this:

java -classpath "lib\mysql-connector-java-5.1.7-bin.jar;lib\mail.jar;GlobalTick.jar" globaltick.Main

And if that works, the following should also work:

java -classpath "lib\mysql-connector-java-5.1.7-bin.jar;lib\mail.jar" -jar GlobalTick.jar

And if it doesnt work, you might have manifest file packed in the GlobalTick.jar in which the classpath is overriden. Check it out.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 20
  • 18
Tackle projects and never again get stuck behind a technical roadblock.
Join Now