Compiled OK but NoClassDefFoundError when running - java

I'm trying to send an email using JavaMail API.
I have jdk 1.5 installed in my home directory from self-extracting binary. I'm on Ubintu 9.10
I compile the program using the next command:
~/jdk1.5.0_22/bin/javac -classpath ~/jdk1.5.0_22/jre/lib/javamail-1.4.3/mail.jar:~/jdk1.5.0_22/jre/lib/jaf-1.1.1/activation.jar hw.java
It compiles OK. As you can see I have specified the path to mail.jar and activation.jar
Now I try to run the app using the next command:
~/jdk1.5.0_22/bin/java -classpath ~/jdk1.5.0_22/jre/lib/jaf-1.1.1/activation.jar:~/jdk1.5.0_22/jre/lib/javamail-1.4.3/mail.jar:. HelloWorldApp
I get an exception java.lang.NoClassDefFoundError: javax/mail/Address
Why can it find classes when compiling and cannot do it when running?
How to correctly run my app?
Thanks in advance

That class should come from your mail.jar. I'm not sure your classpath is being parsed properly. The tilde (~) is a shell function and needs expanding before being sent to the Java process. Have you tried removing the ~ and replacing with /home/{whatever} ? I suspect that's the issue.

Related

Java code runs through IDE but fails from terminal

I have a Java Code where I am able to run it on Intellij using custom configuration. The configuration has following attributes :-
module : java 8 (Oracle OpenJDK 1.8.0_321)
classpath : -cp XYZ.main()
main : com.ABC.XYZ.ManageTraffic
CLI arguements : server XYZ.yml
But when I try to run the jar that was build using gradle from terminal , it gives me Error , could not find or load main class com.ABC.XYZ.ManageTraffic
So far I have tried the following things looking at other solutions at Stackoverflow , still getting the same error
java -jar ques.jar
java -jar ques.jar com.ABC.XYZ.ManageTraffic
java -cp /build/libs/ques.jar com.ABC.XYZ.ManageTraffic
Just to cross check , I unzipped the creataed jar and found that com.ABC.XYZ.ManageTraffic class file is available there , still getting error. What could be the issue?
Run it from the IDE, and while it is running, try to get the command used by the IDE from the process list.
not sure what OS you are using but something like this should work on linux/mac:
ps -ef | grep java
After you have the command you can try to understand why its not working for you, or just use that command
Just want to add how I managed to run it. I created a new shaded jar file of the same application. refreshed its dependencies and now it works. I am yet to find out how creating a shaded jar instead of normal jar helped. Right now the only reason I could figure out is there may be version clashes with some dependencies but I wonder how it could throw could not found main class error.
Anyways , then I ran the file with the following command from terminal:
java -jar ./build/libs/ques-shaded.jar server XYZ.yml

Code runs but doesn't open when converted to .jar file? [duplicate]

I want to execute my program without using an IDE.
I've created a jar file and an exectuable jar file. When
I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:
Error: Unable to access jarfile <path>
I use the command: java -jar Calculator.jar
How I created the jar:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
I had encountered this issue when I had run my Jar file as
java -jar TestJar
instead of
java -jar TestJar.jar
Missing the extension .jar also causes this issue.
Fixed
I just placed it in a different folder and it worked.
[Possibly Windows only]
Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:
java -jar myjar.jar
I was able to fix this by givng the full, quoted path to the jar:
java -jar "%~dp0\myjar.jar"
Credit goes to this answer for setting me on the right path....
I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.
I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:
java -jar C:\Path to File\myJar.jar
i had to write
java -jar "C:\Path to File\myJar.jar"
Just came across the same problem trying to make a bad USB...
I tried to run this command in admin cmd
java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
But got this error:
Error: unable to access jarfile c:\fw\ducky\duckencode.jar
Solution
1st step
Right click the jarfile in question. Click properties.
Click the unblock tab in bottom right corner.
The file was blocked, because it was downloaded and not created on my PC.
2nd step
In the cmd I changed the directory to where the jar file is located.
cd C:\fw\ducky\
Then I typed dir and saw the file was named duckencode.jar.jar
So in cmd I changed the original command to reference the file with .jar.jar
java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
That command executed without error messages and the inject.bin I was trying to create was now located in the directory.
Hope this helps.
None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.
Fixes:
Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.
The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.
Do the following steps:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
Then type java -jar filename.jar
This should work.
Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
java filename.java
If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.
to check this you can view extended attributes on the file
$ ls -l#
-rw-r--r--# 1 dave staff 17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms 619
com.apple.quarantine 68
You can then clear the attributes with
xattr -c file.jar
It can also happen if you don't properly supply your list of parameters. Here's what I was doing:
java -jar test#gmail.com testing_subject file.txt test_send_emails.jar
Instead of the correct version:
java -jar test_send_emails.jar test#gmail.com testing_subject file.txt
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
For me it happens if you use native Polish chars in foldername that is in the PATH.
So maybe using untypical chars was the reason of the problem.
sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work
My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.
Disregarding relative file paths fixed it right up.
In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved
It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ".
For example:
java -jar c:\my folder\x.jar -->
java -jar "c:\my folder\x.jar"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.
Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.
"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar
If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).
A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?
For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.
So i used:
%Java8_64%\bin\java -jar myjar.jar
Instead of java 7 version:
java -jar myjar.jar
I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.
Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.
this is because you are looking for the file in the wrong path
1. look for the path of the folder where you placed the file
2. change the directory cd in cmd use the right path
I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!
Maybe you have specified the wrong version of your jar.
I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
Rename the jar file and try
Explanation :
yes, I know there are many answers still I want to add one point here which I faced.
I built the jar and I moved it into the server where I deploy (This is the normal process)
here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue.
maybe at the time of overriding there can be a permission copy issue.
Hope this will help someone.
Have you tried to run it under administrator privoleges?
meaning, running the command in "Run As" and then select administrator with proper admin credentials
worked for me
I was trying this:
After giving the file read, write, execute priviledges:
chmod 777 java-repl.jar
alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"
Unable to access bla bla..., this was on Mac OS though
So I tried this:
alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"
This did not work "Unable to access jarfile"
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"
This does work
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"
The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.

Compilation using Pi4j

I run into the following problem: Upon running of the GPIO-example for my device built-in in Pi4J, I get an JNI error, followed by a NoClassDefFoundError for the com/pi4j/io/gpio/GpioProvider.
After some searching (both here and other websites) I came to the conclusion that I was missing the pi4j-gpio-extension.jar. Turns out I have to include these specifically while compiling. I was using:
pi4j --compile Gpioblabla.java
which is a macro/shorthand/dont know for
+ javac -classpath '.:classes:*classes:/opt/pi4j/lib/*' -d . Gpioblabla.java
This successfully compiles.
After running the program I get the NoClassDefError.
So the question is, how to explicitly include certain .jar files in pi4j/javac compilation?
Found the error. You need to run it with the classpath as well. So run it like:
java -classpath '.:classes:*classes:/opt/pi4j/lib/*' Gpioblabla

Can't compile GWT application on AWS instance (Linux)

I'm getting an error when trying to compile a GWT application on an AWS EC2 Instance under AMI Linux.
I already installed Java and have it in directory /usr/lib/jvm/java-1.7.0-openjdk.x86_64.
I also set up the enviroment variables:
$JAVA_HOME = /usr/lib/jvm/java-1.7.0-openjdk.x86_64
$PATH = /sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin;/usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin
So, when I try to run the command:
java -cp
"/var/lib/gwt/gwt-2.6.1/gwt-dev.jar;/var/my-project-source-folder"
com.google.gwt.dev.Compiler com.mypackage.MyClass
I'm getting this error:
Error: Could not find or load main class com.google.gwt.dev.Compiler
I downloaded and unziped GWT 2.6.1 again but the files are fine. The same command is working perfectly in my Windows machine. Do you know what's happening?
Ok, this is embarrassing.
Here is the answer:
I was separating the paths with ; (Windows) instead of : (Linux). BTW, who would think in separate things with :, you Linux guys know how to make hard the easy...

Getting error "bash: appletviewer: command not found".

I'm trying to learn Java and I'm having problems with the appletviewer command. I am using openSUSE 11 and am able to compile and run normal java programs but when I issue the appletviewer command I'm getting the following error "bash: appletviewer: command not found".
I have set the PATH variable in the .bashrc file. so the problem is not that of the path. This can also be verified from the fact that javac and java commands are working normally. I have googled and tried many said resolution but none is working. I have even tried moving the files to the bin folder and running the command from there.
So what may the cause of this? Isn't appletviewer designed to work in linux? And FYI I have replaced the OpenSDK and IcedTea versions with the jdk1.6.0_14 version from the sun site. Please help.
Thanks
Chris
As a test, do the following:
$which java
$which appletviewer
Check to see if the paths are different. If the second command gives no output, appletviewer may not even be installed (or can't be located by your system).
You need to install java-1_6_0-sun-devel-1.6.0.u14-0.2.1
$ sudo zypper install java-1_6_0-sun-devel
You will find it in the openSUSE-11.1-Non-Oss repository.

Categories

Resources