Reputation: 71
I am new to java programming and I have created a program which integrates selenium, apachepoi and java swing. While compiling the program I was able to compile it successfully and the program does run right; however when I tried making a jar file for my program it shows java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
error.
Below is my program's folder structure
c:\users\userid\documents\java\crazyrunner
The java file is with name CrazyRunner.java
within crazyrunner folder
The program is within a package with name crazyrunner (first line of the program starts with package crazyrunner
)
Command used to compile (not sure whether it is relevant)
javac -encoding UTF8 crazyrunner\CrazyRunner.java
Compilation resulted in creation of .class files in both parent (java) and child (crazyrunner) folders
Command used to run
java crazyrunner.CrazyRunner
(This worked just fine)
Command used generate the jar
jar cvfm CrazyRunner.jar manifest.mf *.class crazyrunner\*.class
The result of the jar command is successfull and all class files within crazyrunner and outside crazyrunner (within the parent folder 'Java') are added to the jar (CrazyRunner.jar
)
The manifest.mf file has data as below
Manifest-Version: 1.0
Created-By: Eric Stanley
Main-Class: crazyrunner.CrazyRunner
Class-Path: "C:\poi-3.10-beta2\poi-3.10-beta2-20130904.jar"
"C:\poi-3.10-beta2\poi-examples-3.10-beta2-20130904.jar"
"C:\poi-3.10-beta2\poi-excelant-3.10-beta2-20130904.jar"
..
Ends with 2 new lines
Not sure about what am I missing :-( and I have spent a whole damn day fixing this and left without choice but to post this q!! and yes I did tried all options that stackoverflow already has and nothing worked out :-(
Option 1:
Open the Control Panel
Go to System
Go to Advanced Systems Properties
Then Environment Variables
In System Variables, click Add
New Variable Name: _JAVA_OPTIONS
New Variable Value: -Xmx512M (tried -Xmx1024M too)
Click OK
Option 2:
Reinstall Java
Option 3:
Open the Run box
Enter msconfig
Services (tab)
CHECK "Hide All Microsoft Services"
Click "Disable All" (button)
Click APPLY
Click OK
Option 4:
Update manifest.txt file with classpath
Help is much appreciated
Note:
The program opens up a GUI (while I enter java crazyrunner.CrazyRunner
) and when I tried giving CrazyRunner.jar
alone in the command prompt, it throws an error stating Java Virtual Machile Launcher. A java exception occurred
PS:
I am using Windows 8 and the version of java is 1.7.0_51 and I am not using any IDE and I do have a hunch that this might be due to too many jar files in the classpath. If yes, fix for that is badly needed!!
Upvotes: 0
Views: 5670
Reputation: 71
Thanks for all your responses and at last I found the answer. Gradle!! :-)
As I assumed, there was no change to the already created .jar file; meaning the jar file that I created with the manifest file was right and the issue is that the class files that are available during compile time were not available during run time. Hence, I used all the jar files that were used for compilation and also my already created jar file (CrazyRunner.jar
) to build my new jar file using gradle.
Steps followed:
Downloaded gradle 1.10 from http://downloads.gradle.org/distributions/gradle-1.10-all.zip
Extracted the files under "C:\gradle-1.10
Opened cmd prompt in administrator mode and typed
"C:\Windows\system32\rundll32.exe" sysdm.cpl,EditEnvironmentVariables
Added GRADLE_HOME
environment variable with value as C:\gradle-1.10\bin
(under system variables section)
Added %GRADLE_HOME%
to the PATH
variable (under system variables section)
Added JAVA_OPTS
environment variable with value -Xms256m -Xmx2048m
Created a new file with name build.gradle
into the project folder (crazyrunner)
Wrote the below code in build.gradle
file
apply plugin: 'java'
sourceCompatibility = 1.6
targetCompatibility = 1.8
defaultTasks = ['clean', 'jar']
dependencies {
compile fileTree(dir: 'corelib', includes: ['*.jar'])
compile fileTree(dir: 'libs', includes: ['*.jar'])
}
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest { attributes 'Main-Class': 'CrazyRunner' }
}
Copied all jar files (just searched by .jar) from C:\poi-3.10-beta2
and C:\selenium-2.39.0
and pasted into a separate folder (named libs
) within my project folder (within crazyrunner)
Copied my already created jar(CrazyRunner.jar
that was created as mentioned in the original question) file into a separate folder (named corelib
) within my project folder (within crazyrunner)
Note: At this point, my project folder (crazyrunner) had 2 sub-folders (libs
and corelibs
) and 1 file (build.gradle
)
Opened command prompt (normal mode) and traversed to the project folder (crazyrunner)
Typed command gradle build
Thatz it!!
It took around 20 mins for me to complete the build successfully and after a long night search I found it working!!
My Learning:
Gradle basically integrates all the compiled files within the jar (if built as above, like the build.gradle
file), so that the newly created jar file doesn't need any extra dependencies during runtime which is an advantage; however, since all dependent files are added to the jar, it makes the size huge but the file runs in any system even without selenium and apache-poi (I hope ;-))
Thanks y'all and my special thanks to jbaliuka :-) Nice work guys!!
Upvotes: 1
Reputation:
A few things that you could check is the manifest file whether it has follow the correct format as in: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Note that the classpath uses slash / instead of backslash \, and multiple jar files are concatenated by a space character, NOT by a newline. E.g. something like following would be a valid manifest file:
Manifest-Version: 1.0
Created-By: Eric Stanley
Main-Class: crazyrunner.CrazyRunner
Class-Path: "C:/poi-3.10-beta2/poi-3.10-beta2-20130904.jar" "C:/poi-3.10-beta2/poi-examples-3.10-beta2-20130904.jar" "C:/poi-3.10-beta2/poi-excelant-3.10-beta2-20130904.jar"
And also make sure that the jar files location are correctly spelled up to their lower and upper case.
Upvotes: 0