I am getting ClassNotFoundException when I try to run a jar whith below command:
java -jar MyJar.jar
I created the jar with eclipe. MyJar depends of a couple of other jars. I saw in ecplise that all these other jars are there in the classpath.
I also added these jars to the classpath in Unix using export classpath. But still I get the ClassNotFoundException.
Exception Stack Trace:
Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.hadoop.conf.Configuration
at com.a.HDFSCopy.readURI(HDFSCopy.java:16)
at com.a.CopyMain.main(CopyMain.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
... 2 more
Class Path Before:
/opt/ibm/biginsights/IHC/lib/biginsights-gpfs-1.1.1.jar:/opt/ibm/biginsights/IHC/hadoop-core.jar:
Class Path After (included the locations of the jars needed for dependencies) :
[biadmin#big-instght-15 ~]$ echo $CLASSPATH
/opt/ibm/biginsights/IHC/lib/biginsights-gpfs-1.1.1.jar:/opt/ibm/biginsights/IHC/hadoop-core.jar::/home/biadmin/hadoop_jars/commons-logging-1.1.1.jar:/home/biadmin/hadoop_jars/commons-configuration-1.8.jar:/home/biadmin/hadoop_jars/commons-lang-2.4.jar
But, I realized that if I open a new terminal and echo $CLASSPATH, it displays only the Class Path Before. It doesn't reflect the changes I made to the classpath. i.e. it doesn't show Class Path After.
How to fix this?
Thanks,
Mahalakshmi
What is the main class listed in MANIFEST.MF? If you unjar the jar, is it in the jarfile in the correct location?
Related
I have a moderately old, small Java application which has an option to read and export PDF files using the Apache PDFBox library (hereunder, "pdfbox-app.jar"). All the files, including this resource, are stored in a single flat folder.
This works fine when called from a JAR file:
D:\Prog\!GitHub\Arena>java -jar Athena.jar NPCGenerator -p
OED NPC Generator
-----------------
Writing Gwenllian-ElfFtr1Wiz1.pdf
It similarly works fine when run from my IDE (jGrasp).
But it fails when called from the command line, outside of its JAR:
D:\Prog\!GitHub\Arena>java NPCGenerator -p
OED NPC Generator
-----------------
Writing Eoin-HalflingFtr1.pdf
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDoc
ument
at CharacterPDF.writePDF(CharacterPDF.java:49)
at NPCGenerator.printToPDF(NPCGenerator.java:294)
at NPCGenerator.makeAllNPCs(NPCGenerator.java:270)
at NPCGenerator.main(NPCGenerator.java:308)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.j
ava:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoader
s.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 4 more
What should I be doing to run this on the command line outside of its own JAR?
You should put the pdfbox jar, and any other dependencies, on the classpath:
java -classpath .;pdfbox-app.jar NPCGenerator -p
Without that, Java doesn't know where to look for org/apache/pdfbox classes. It looks for .class files relative to the default classpath (which is just ., the current directory), but does not look inside jars.
please I need help. I'm trying to package my maven build and also include environment variables but my program cannot find the .env file. How can I resolve this issue? I have been using eclipse, but I don't know where I can configure the path to the .env file. I used io.github.cdimascio to read environment variables. The program works fine on my IDE. The trouble starts when I run the program outside the IDE. What should I do?
Exception in thread "Thread-1" Exception in thread "Thread-0" java.lang.ExceptionInInitializerError
at com.arcalitegames.Main$2.run(Main.java:29)
at java.lang.Thread.run(Unknown Source)
Caused by: io.github.cdimascio.dotenv.DotEnvException: Could not find ./.env on the classpath
at io.github.cdimascio.dotenv.internal.ClasspathHelper.loadFileFromClasspath(ClassPathHelper.kt:37)
at io.github.cdimascio.dotenv.internal.DotenvReader.read(DotenvReader.kt:36)
at io.github.cdimascio.dotenv.internal.DotenvParser.parse(DotenvParser.kt:26)
at io.github.cdimascio.dotenv.DotenvBuilder.load(Dotenv.kt:119)
at com.arcalitegames.connector.Devs.getDotenv(Devs.java:10)
at com.arcalitegames.connector.Connector.<clinit>(Connector.java:14)
... 2 more
I had this same problem when I tried to deploy my program to Glassfish. You need to specify a location on the file system like ~ and place your .env file there.
Here is an example on how to do this:
Dotenv env = Dotenv.configure().directory(System.getProperty("user.home")).load();
Dotenv should now be able to find the file if you've placed it at ~/.env.
JAR file will not run, instead returns
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
I've tried deleting the class file, then re-creating the JAR file. I've tried deleting and re-creating the program from scratch. I've tried creating the JAR file without a manifest.
package ExampleProject;
public class ExampleProgram {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is the exact process in how I tried to create the JAR file on the command-line.
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java
Josephs-MBP:program jepappano4$ java ExampleProgram
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java manifest.mf
Josephs-MBP:program jepappano4$ jar -cvfm myprogram.jar manifest.mf *.class
added manifest
adding: ExampleProgram.class(in = 450) (out= 301)(deflated 33%)
Josephs-MBP:program jepappano4$ java -jar myprogram.jar
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
I am following a tutorial on creating JAR files and I expected this process to work, but it doesn't. What am I doing incorrect here?
Not sure if this is important or not, however, I am using Java 11.
The 'wrong name' error is the key error here. This is what it means:
In java, the full name of a class is its package, followed by a dot, followed by the classname. So, your full name for this class is ExampleProject.ExampleProgram.
To start them, you must put this full name in yor manifest: It needs to have an entry that looks like:
Main-Class: ExampleProject.ExampleProgram
In addition, folder structure must match the dots. So, the jar file (which is really just a zip file) must contain inside it at the root level a directory named ExampleProject and this dir needs a file named ExampleProgram.class.
You've gotten one or both of those wrong, causing this error. You can check the structure of your jar file as follows:
jar tvf myjar.jar
you should see a listing for:
ExampleProject/ExampleProgram.class
in that precise location.
When I use the javapacker command, I get a NoSuchFileException when it should copy the libraries to the app folder. I'm running the following command:
javapackager -deploy -nosign -v -native image -name test
-appclass test.Test
-srcdir "E:\projects\test\target"
-srcfiles test-1.0.jar;lib
-outdir "E:\projects\test\target\dist"
Anything seams to work fine, but i get the NoSuchFileException inside the output and the libraries inside the lib folder are missing (did not get copied).
Running [D:\Java64\jdk-9.0.4\bin\java.exe, -version]
Creating app bundle: test in E:\projects\test\target\dist
"Adding modules: [java.rmi, java.sql, javafx.web, ... long list ...
outputDir = E:\projects\test\target\dist\test\runtime
modulePath = [D:\Java64\jdk-9.0.4\jmods]
addModules = [java.rmi, java.sql, javafx.web, ... long list ...
limitModules = []
excludeFileList = .*\.diz
stripNativeCommands = true
userArguments = {}
Using default package resource [icon] (add package/windows/test.ico to the class path to customize)
Using default package resource [Template for creating executable properties file.] (add package/windows\test.properties to the class path to customize)
Running [D:\Java64\jdk-9.0.4\bin\javapackager.exe, --icon-swap, C:\Users\XXX\AppData\Local\Temp\fxbundler15945263774290817340\windows\test.ico, E:\projects\test\target\dist\test\test.exe]
Icon File Name: C:\Users\XXX\AppData\Local\Temp\fxbundler15945263774290817340\windows\test.ico
Executable File Name: E:\projects\test\target\dist\test\test.exe
Running [D:\Java64\jdk-9.0.4\bin\javapackager.exe, --version-swap, C:\Users\XXX\AppData\Local\Temp\fxbundler15945263774290817340\windows\test.properties, E:\projects\test\target\dist\test\test.exe]
Resource File Name: C:\Users\XXX\AppData\Local\Temp\fxbundler15945263774290817340\windows\test.properties
Executable File Name: E:\projects\test\target\dist\test\test.exe
Exception: java.nio.file.NoSuchFileException: E:\projects\test\target\lib\animal-sniffer-annotations-1.14.jar -> E:\projects\test\target\dist\test\app\lib\animal-sniffer-annotations-1.14.jar
Config files are saved to C:\Users\XXX\AppData\Local\Temp\fxbundler15945263774290817340\windows. Use them to customize package.
Result application bundle: E:\projects\test\target\dist
I don't know what the following line of the output is trying to tell me. The file clearly exists inside my lib folder and the javapackager did find it itself by looking inside the lib folder, that I provided with the -srcfiles test-1.0.jar;lib argument.
Exception: java.nio.file.NoSuchFileException: E:\projects\test\target\lib\animal-sniffer-annotations-1.14.jar -> E:\projects\test\target\dist\test\app\lib\animal-sniffer-annotations-1.14.jar
Any idea why javapacker complains that the file would not exist?
As per https://docs.oracle.com/javase/9/tools/javapackager.htm#JSWOR719,
-srcfiles should be provided with a list of files. lib is a directory. I would try using -srcdir.
Also, if you get
Exception: java.lang.Exception: Error: Modules are not allowed in srcfiles
it means you have to use --module-path, and then set the main module using -m <module name>
I am trying to run an application that reads and writes to the amazon dynamo DB. I downloaded the Eclipse toolkit and AWS SDK and if I run the application from my local PC it works perfectly. Next, I exported it to a jar file and uploaded it to my EC2 instance. However, when I run it there I get an error.
/home/apps/java/database/bin$ java -jar myJar.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/auth/AW SCredentials
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: DynamoDB. Program will exit.
I assume it has to do with the classpath, but in /home/ubuntu/.bashrc I have set it as such:
CLASSPATH="./:/home/apps/java/database/bin/*:/home/apps/java/database/bin/aws-java-sdk-1.3.12.jar"
export CLASSPATH
/home/apps/java/database/bin contains all the .jar files that are in the AWS SDK:
aspectjrt.jar
aspectjweaver.jar
aws-java-sdk-1.3.12.jar
aws-java-sdk-1.3.12-javadoc.jar
aws-java-sdk-1.3.12-sources.jar
aws-java-sdk-flow-build-tools-1.3.12.jar
commons-codec-1.3.jar
commons-logging-1.1.1.jar
freemarker-2.3.18.jar
httpclient-4.1.1.jar
httpcore-4.1.jar
jackson-core-asl-1.8.7.jar
jackson-mapper-asl-1.8.7.jar
mail-1.4.3.jar
myJar.jar
spring-beans-3.0.7.jar
spring-context-3.0.7.jar
spring-core-3.0.7.jar
stax-1.2.0.jar
stax-api-1.0.1.jar
What am I missing?? I have been looking at this for a day and a half. Thank you in advance!!
The classpath entry of "/directory/*" may be messing things up. Classpaths are separated with colons, but asterisk expansion gives spaces. Try this little shell script to start it.
#!/bin/sh
JAVA_OPTS="-Xms256M -Xmx4G"
CP=`find /home/apps/java/database/bin/*jar -exec echo -n "{}:" \;`
java -cp ${CP%?} -jar yourjar.jar
A couple of notes:
the JAVA_OPTS is only there as a reminder that you may need more memory than the default.
the crazy syntax for CP on the final line strips the last character, since the "find" line is leaving a colon on the end.
You may want to include your jar and launch the correct class if it isn't an executable jar.
Hope this helps!
This list of libs works for me
aws-java-sdk-1.11.285-javadoc.jar
aws-java-sdk-1.11.285-sources.jar
aws-java-sdk-1.11.285.jar
aws-java-sdk.jar
aspectjrt-1.8.2.jar
aspectjweaver.jar
aws-swf-build-tools-1.1.jar
commons-codec-1.9.jar
commons-logging-1.1.3.jar
freemarker-2.3.9.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
ion-java-1.0.2.jar
jackson-annotations-2.6.0.jarÃ…
jackson-core-2.6.7.jar
jackson-databind-2.6.7.1.jar
jackson-dataformat-cbor-2.6.7.jar
javax.mail-api-1.4.6.jar
jmespath-java-1.11.285.jar
joda-time-2.8.1.jar
netty-buffer-4.1.17.Final.jar
netty-codec-4.1.17.Final.jar
netty-codec-http-4.1.17.Final.jar
netty-common-4.1.17.Final.jar
netty-handler-4.1.17.Final.jar
netty-resolver-4.1.17.Final.jar
netty-transport-4.1.17.Final.jar
spring-beans-3.0.7.RELEASE.jar
spring-context-3.0.7.RELEASE.jar
spring-core-3.0.7.RELEASE.jar
spring-test-3.0.7.RELEASE.jar