Conflict between MySQL and mail Java libraries - java

I have a problem with Java libraries. I'm using javax.mail and mysql-connector.
While compiling I don't have any problems, but if I try to execute the program with sudo:
$sudo java Server -jar mysql-connector-java-5.1.28.jar
It gives me this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at dbConnect.dbQuery(dbConnect.java:192)
at Server.main(Server.java:39)
while if I try to execute it without sudo:
$java Server -jar mysql-connector-java-5.1.28.jar
It gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/internet/AddressException
at dbConnect.registration(dbConnect.java:161)
at dbConnect.splitUsrPass(dbConnect.java:87)
at dbConnect.dbQuery(dbConnect.java:196)
at Server.main(Server.java:39)
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
I can understand why it doesn't work without administrator privileges (I guess the mail library requires them) but it seems strange to me that it won't find the MySQL library if I give it the administrator privileges.
Does anyone know if this is a known issue?

When you want to run something with Java on the command line, you have two options:
Run a class: java MyClass
Run a jar: java -jar MyJar.jar
What you are trying to do is both, which will result in the -jar-part will be ignored.
I'm guessing that what you want to do is run the class Server, with the MySQL-jar on the classpath. The correct command will then be:
java Server -cp mysql-connector-java-5.1.28.jar
You could also add the directory containing the MySQL-jar to the CLASSPATH environment variable. The reason you get different error messages when using sudo and not is probably because the root user has a different CLASSPATH than your user (your user lacks the javax.mail-jar on the classpath, while root has it.)

Related

Failing to start java program ClassNotFound exception received

Trying to start java program on linux but it fails to locate JSoup jar file. I have checked other questions and found on how to add libraries while compiling and while trying to run it but I am not sure if I am doing it correctly because it still fails to load the library....
I enter the following commands:
root#vps5441:/var/www/var/www/TimetableLoader# javac -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. TimetableLoader.java
root#vps5441:/var/www/var/www/TimetableLoader# java -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. -classpath /var/www/var/www/TimetableLoader/ TimetableLoader
And I get the following error when the program tries to access the JSoup library.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at TimetableLoader.main(TimetableLoader.java:46)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
I assume jsoup-1.8.1.jar is in the current directory?
You don't generally need both -classpath and -cp. It's probably ignoring the values given by -cp as a result.
Put your own classes ahead of the 3rd-party ones.
You're already in /var/www/var/www/TimetableLoader so keep it simple:-
root#vps5441:/var/www/var/www/TimetableLoader# java -cp .:jsoup-1.8.1.jar:mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar TimetableLoader
(Best to get out of the habit of running things as "root" unless you have a genuine need, just sayin')

Raspberry pi - java - Serial Com

I've got a serious problem with my Raspberry Pi (OS: Raspbian) and Java (JDK-7-Armhf). My code wont execute without creating a Exception.
I'll been reading and trying several proposals and nothing have worked yet.
So now i am confused of what went wrong..?
So it goes like this:
I've got a Java source code that will run in my Eclipes IDE.
But when i export the .jar-file, even with library (JRE System lib. _86, JavaSE1.7, JAVAX.comm), as "Runnabel JAR" or just .JAR and execute it om my Raspberry pi, i've got "NoCLassDefFoundExecption: SerialPortEventListener..!
I just don't know why, it won't find the library and use it..!
On my Pi i have librxtx-java installed and JDK-7-oracle-armhf.
librxtx-java should be setting its JAVA_PATH on it's own when installed, but not sure that is done correct!
(I've got a folder /usr/jni and it contains: librxtxSerial.so and
/usr/lib/jvm/jdk-7-oracle-armhf/).
Looking at the library Javax.RXTXcomm in Eclipse, i've got RXTXcomm.jar, that contains SerialPortEventListener, so it is defininated i the .jar.
I'm thinking about, could it be, that there is a missing link between my Java JVM and linux serialport driver? My JRE is working in some way, because it will execute a code to readout HOSTNAME & IP-Adress.
So is there some one who knows how to fix this..?
This is a readout of Java Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: javax.comm.SerialPortEventListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
A copy of my code. "SimpleRead.java" The original from java comm API samples
You have to change the reference from javax.comm to gnu.io in your source code.
Try following:
Switch to root and run the program you created
If that helps do following
sudo adduser username dialout
then log out and log in from all terminals and GUIs

Java running code from command line

I'm on Mac OS X 10.9.2 (Mavericks). I have code that uses an external Java library (twitter4j). It runs fine when I run it through NetBeans. However, trying to run almost identical code in the terminal gives me errors.
My directory structure is straightforward- I have a 'src' folder with the .java file and a 'lib' folder with the external .jar files I use.
From the src folder, I call javac -cp "../lib/*" MyProgram.java which seems to work alright. Now, if I call java MyProgram, I get an exception:
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/StreamListener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: twitter4j.StreamListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
It seems to have trouble properly importing the external twitter4j library. What am I missing here?
These are my import statements in the code:
import twitter4j.*;, import twitter4j.conf.ConfigurationBuilder;
UPDATE: Using suggestions below, I also tried running it via java -cp "../lib/*" MyProgram which gives: "Could not find or load main class MyProgram"
You need to specify the classpath when running the program as well as when compiling it:
E.g.
java -cp "../lib/*" MyProgram

Error running spoon on Ubuntu 14.04 64 bit

I am using Spoon tool of Pentaho data integration for long and it was working fine on my system. But since i moved it to /opt I am unable to run again . I have Oracle Java 8 installed on my system and each time try to run it i am end up with following exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getMethod0(Class.java:2937)
at java.lang.Class.getMethod(Class.java:1771)
at org.pentaho.commons.launcher.Launcher.main(Launcher.java:149)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Composite
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
please help me to resolve this error i haven't found any solution yet
I found a solution to this problem I have removed all hidden directories generated by Kettle as well as its copy from /opt . Then I have extracted new version copy . after that I have added /opt/data-integration to my path variable and I have tried to run it from my home . Although it was not a successful run but it has generated all those dependent hidden folders required to run it . then I have to go to that directory by issuing
cd /opt/data-integration
and then I was successful to run it by issuing
sh spoon.sh
I have to go to that directory because Pentaho developers has set it so by placing relative path to launcher folder in their main command at spoon.sh.

Java program executes in eclipse but not in terminal

I am using Eclipse with Eclipse Maven Plugin (m2e).
My java program compiles and run correctly from eclipse interface but I am unable to compile and run it from terminal.
My Eclipse Setting:
I am using two third party APIs, for which in eclipse build path I added
"/home/syed/workspace/FirstMaven/target/resources/fuse-jna-master/build/classes" (as external class folder)
"/home/syed/workspace/FirstMaven/target/resources/apache-jena-2.11.1/lib" (as external jars)
Package:
package org.organization.upesh.FirstMaven;
My Project path:
syed#ubuntu:~/workspace/FirstMaven$
Source Code Directory Path:
syed#ubuntu:~/workspace/FirstMaven/src/main/java/org/organization/upesh/FirstMaven$
Classes Directory:
syed#ubuntu:~/workspace/FirstMaven/target/classes/org/organization/upesh/FirstMaven$
When I try to execute myProgram via below command
syed#ubuntu:~/workspace/FirstMaven/target/classes$ java org.organization.upesh.FirstMaven.myProgram
it gives me these errors:
Exception in thread "main" java.lang.NoClassDefFoundError: net/fusejna/util/FuseFilesystemAdapterFull
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: net.fusejna.util.FuseFilesystemAdapterFull
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
But my test program that do not use the third party API runs correctly via:
syed#ubuntu:~/workspace/FirstMaven/target/classes$ java org.organization.upesh.FirstMaven.test
I think myProgram is not executing because of the two APIs (class folder and jar folder) that I am using.
I have added path of class and jar folders of the APIs to /etc/environment (given below) and rested my computer, but still the same error
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/syed/workspace/FirstMaven/target/resources/apache-jena-2.11.1/lib:/home/syed/workspace/FirstMaven/target/resources/fuse-jna-master/build/classes"
Please guide me how to run my program correctly
JVM does not get libraries from PATH. It uses special environment variable CLASSPATH that can contain a list of directories or jar files separated by colon on Unix or semicolon on Windows.
So, just define CLASSPATH and put references to all your libraries there.
Aleternatively (and IMHO better) use command line switch -classpath (or its alias -cp) when running java:
java -cp mylib1.jar:mylib2.jar com.mycompany.Main

Categories

Resources