I am trying to run a program on the command line that uses the JavaMail API. This program also reads in a message from a text file. However, I'm getting this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Originally I used java -cp /path/to/mail.jar mailTest < message.txt
I configured the classpath for the jar successfully (as far as I know). Can anyone give me some pointers? :/
Your classpath is definitely wrong. The classpath needs to include both the javax.mail.jar file and the classes for your application. Exactly where is the JavaMail jar file, exactly where are your application classes, and exactly what java command line did you use? If your application classes are in the current directory, you need something like
java -cp /path/to/javax.mail.jar:. mailTest
Actually if you look at the error message:
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
That's probably because your jar depends on another jar (probably mail-x.y.z.jar) and you did not include it on your classpath when trying to run your jar.
Either include the missing jar(s) on the command line, see this post, or add them to your manifest file, see the doc
Related
My code compiles and runs without issue in IntelliJ. However when I try building the program as a JAR and running in cmd, I get the following error.
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/log4j/Layout
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 7 more
I believe I am creating the JAR correctly. Here are the libs I am bundling in.
I create the JAR by adding an empty JAR to the artifacts tab, then adding the META-INF, libs, and code manually.
As you can see, lib is contained inside which does hav log4j and apache commons.
Note this issue only pops up when the JARs are created from IntelliJ. If I create a JAR via eclipse the JAR works just fine, but that isn't viable.
Since StackExchange advised me to not ask for help or clarification on a previously existing post, I am making a new thread.
On Windows 10x64 I downloaded the Eclipse SWT (in second-to-last section of Downloads page), set my ANDROID_SWT environment variable to <download location>\swt.jar, and have attempted to run uiautomatorviewer from cmd again, only to receive the same error message as when I tried to use the SWT bundled with eclipse:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Listener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Listener
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Has anyone else encountered this error, and if so, what was the root cause?
Thanks in advance for your time and help!
In my case adding ANDROID_SWT to environment variables or editing PATH didn't solve the problem. Therefore I found a different solution via help from this question.
Running uiautomatorviewer.bat and output :
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Control
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Listener
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
Solution :
Go to : https://www.findjar.com/ and search for missing class.
In this case search for class :
org.eclipse.swt.widgets.Control
For your case it is:
org.eclipse.swt.widgets.Listener
Names of the classes in the error can change, but all the classes needed comes from .jar file that will be downloaded.
In the results click :
[CLASS] org.eclipse.swt.widgets.Control
or for your case :
[CLASS] org.eclipse.swt.widgets.Listener
Download latest and suitable file for your OS. This was the latest and suitable for Windowsx86_64 currently.
Put downloaded .jar file into :
C:\Users\{userId}\AppData\Local\Android\Sdk\tools\lib
or equivalent path for your OS.
Now uiautomatorviewer.bat should run without any problems.
Make sure JDK location selected in Android studio is same as JDK set in environment variable.
In my case the exception was
Exception in thread "main" java.lang.NoClassDefFoundError:
org/eclipse/swt/custom/TableTreeItem
at org.eclipse.jface.util.OpenStrategy.initializeHandler(OpenStrategy.java:270)
at org.eclipse.jface.util.OpenStrategy.<init>(OpenStrategy.java:108)
at org.eclipse.jface.viewers.StructuredViewer.hookControl(StructuredViewer.java:1213)
at org.eclipse.jface.viewers.ColumnViewer.hookControl(ColumnViewer.java:68)
at org.eclipse.jface.viewers.AbstractTreeViewer.hookControl(AbstractTreeViewer.java:1452)
at org.eclipse.jface.viewers.TreeViewer.hookControl(TreeViewer.java:274)
at org.eclipse.jface.viewers.TreeViewer.<init>(TreeViewer.java:137)
at org.eclipse.jface.viewers.TreeViewer.<init>(TreeViewer.java:124)
at com.android.uiautomator.UiAutomatorView.<init>(UiAutomatorView.java:361)
at com.android.uiautomator.UiAutomatorViewer.createContents(UiAutomatorViewer.java:66)
at org.eclipse.jface.window.Window.create(Window.java:431)
at org.eclipse.jface.window.Window.open(Window.java:790)
at com.android.uiautomator.UiAutomatorViewer.main(UiAutomatorViewer.java:78)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.custom.TableTreeItem
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 13 more
Steps followed for the solution
Go to https://jar-download.com/
Search for the missing jar file in my case it was org.eclipse.swt.custom.tabletreeitem
Download the jar file compatible with your system Mac/Win/Linux (In my case it was mac
enter image description here
Extract the jar file and copy it
Go to the lib folder inside SDK (Android/Sdk/Tools/Lib
Paste the jar file into the lib folder
Restart your terminal or cmd
Run the command again for cd $ANDROID_HOME/tools/bin
Run the command ./uiautomatorviewer
Alternatively
Try to download the swt jar file with a different version and copy it to the respective folders.
Follow this thread :
https://github.com/android/android-test/issues/911#issuecomment-849389068
Precisely follow these instructions. I made the mistake of trying to download the Eclipse SWT in order to troubleshoot other issues I was having; setting this different SWT version in the CLASSPATH and ANDROID_SWT environment variables caused this exception.
I have extracted a jar from a Maven project that runs the MapReduce job. However, I keep receiving the error "java.lang.ClassNotFoundException". The things that I have tried to use to repair this is:
Configured the classpath
Tried doing job.setJar(.jar)
Attempted job.setJarbyClass(.class)
Changing JobConf path file
Caress Hadoop and tell it everything is going to be okay
I extracted the jar file from Maven and transferred it to a Linux server, and running it from there.
The full error message is:
Exception in thread "main" java.lang.ClassNotFoundException: BLAMapAttempt2
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
If there is any missing information needed to answer this question, please let me know, and thank you for reading.
When exporting the jar out of Eclipse, I unchecked "classpath" because I was not running it locally. This fixed my error.
I am executing the java archive on Spark using spark-submit in Ubuntu. The command is given below. This JAR file was build using Maven Package. The dependencies are specified in pom.xml file.
]$ spark-submit --class HighScore.Driver --master local[*] JarfilePath/Levelwise_PCFS-0.0.1-SNAPSHOT.jar InputFilePath/K9_Site1.csv 1000.
I am getting following error even when packageName.className (HighScore.Driver) is specified in the command.
Here is the error message.
Exception in thread "main" java.lang.NoClassDefFoundError: com/github/lwhite1/tablesaw/api/Table
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:727)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: com.github.lwhite1.tablesaw.api.Table
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
com/github/lwhite1/tablesaw/api/Table dependency was also specified in pom.xml file. But still it throws the exception.
Can some one help me in rectifying this error.
Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath.
NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.
For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class.
Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.
Please refer this post for more details. It would be helpful.
When I execute mycode from intelliJ Idea it works fine but when I run the jar on my server it throws the below error. I am using 'org.mongodb:mongodb-driver:3.4.1' version.
Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/conversions/Bson
at com.myapp.server.mongo.MongoDbHandler.prepareMongoDb(MongoDbHandler.java:68)
at com.myapp.server.mongo.MongoDbHandler.<init>(MongoDbHandler.java:22)
at com.myapp.server.Client.main(Client.java:167)
Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson
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)
... 3 more
I know its an old thread and its just for those who have the similar issues. Few days back I was also facing the same issue with same error trace while running the jar by command line.After rechecking the code found that
It was not a problem of the jar version but its problem of the packaging of the
jar
After packing the jar with all proper dependencies will solve this issue.
I have used
maven-assembly-plugin
for packaging and its works.
.
If I had to guess you need to get yourself whatever BSON library you're using; I wouldn't say that Mongo is your problem.
key line here is:
"Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson"
So it's saying that it's not finding that particular class, I would look in IntelliJ and see what BSON library it is, then put it in your build directory (or java lib) on your server.