I'm build a jar file with code that uses other jar files as dependencies, and I can't get it to run (I keep getting a NoClassDefFoundError).
All the other similar issues I could find online used maven as a solution, which I want to avoid doing. When I run my code like this:
#!/bin/bash
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $#
it runs fine, but when I try:
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" -jar WikipediaWords.jar $#
it complains.
I compile my jar file with all the dependencies inside, though, so I really have no clue why it can't find anything. Ideally, I wouldn't even want to have to add the -cp path to my java execution, and just have the jar file know to use the nested jar dependency files.
If you have any clue about how to solve this (run the jar file without class def error) without the use of Maven, please let me know!
Thanks in advance!
P.S. My building code might be of use:
#!/bin/bash
javac -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" src/WikipediaWords.java
jar cfm src/WikipediaWords.jar src/Manifest.txt -C src/ .
Try running the below command:
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar:WikipediaWords.jar" WikipediaWords $#
OR
java -cp "WikipediaWords.jar:src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $#
Related
I'm trying to run java from the command line, and haven't had to include extra packages with the javac command before and I can't figure out what I'm doing wrong.
I'm running javac -d bin -cp jar1:jar2:...:jarN:PackageName MyClass.java but I'm still getting an error: package PackageName does not exist
I'm using the absolute paths for everything, and I also tried individually listing the java files inside the package but that didn't work either. I'm using a colon since I'm on a mac.
Anyone know what I am doing wrong? Thanks for any help!
I realized that I needed to compile the java files in PackageName before I could compile MyClass.java, which depended on them. So what I needed to do was: javac -d bin -cp jar1:jar2:...:jarN PackageName/*.java and then I could compile MyClass.java with bin added to the classpath as well as the jars.
i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp
I am compiling and running my Java project from the command line rather than eclipse for the first time and have a slight inconvenience that I just can't seem to find a solution too.
My project has a standard structure:
Project Directory
/src
/self
/redway
/myAPP.java
/bin
/libs
So I compiled using:
javac -sourcepath src src/self/redway/myApp.java -d bin
and so far so good...
I can run the program by navigating to the /bin and then just typing
java self.redway.myApp
BUT and this is really annoying. How do I run it from my project root directory?
I tried just
java bin/self.redway.myApp
and some other obvious ideas to no avail. I know it is a minor thing but it is super irritating and I'm sure there is a simple answer which I should have spotted immediately but I just can't find it!
Thanks.
You can add the java -cp option to specify the class path. E.g.
java -cp bin self.redway.myApp
you can write a shell script like the following:
cd [your bin directory]
java self.redway.myApp
call this script for example runProgram.sh, place it in the directory where you want to be when you run your program, and run it using
sh runProgram.sh
that would also save you time if your program is going to use other jars and you have to add them to the class path, then all you have to do is change your java command in the shellscript to become
java -cp [path to the jar]:[path to another jar]:...:[path to your bin folder] self.redway.myApp
I downloaded a Java library with a file titled Makefile, but I do not know what to run it with. It has no extension, and this is the text that appears when I open it in TextEdit:
all: BigDecimalMath.jar Wigner3jGUI.jar html
html:
javadoc -sourcepath . -private -d html org.nevec.rjm
BigDecimalMath.jar:
javac org/nevec/rjm/*.java
jar cf $# org
Wigner3jGUI.jar:
javac org/nevec/rjm/*.java
jar cmf Wigner3jGUI.mf $# org/nevec/rjm/*.class
distclean:
rm -rf html *.jar org/nevec/rjm/*.class
Could anyone help me figure out how to run this file (By the way, I have a Mac running Mavericks if that matters at all)
try two commands
make
java -jar Wigner3jGUI.jar
Makefiles are similar to ant files and are run by the make program. I have not seen make used to build jars (usually c/c++). However, it looks like it will build the jars. The all target should build the jars, which can then be run by the java command.
How to compile all files in directory to *.class files?
Well, this seems pretty obvious, so I may be missing something
javac *.java
(With appropriate library references etc.)
Or perhaps:
javac -d bin *.java
to javac create the right directory structure for the output.
Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)?
Yet another way using "find" on UNIX is described here:
http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html
The following two commands will compile all .java files contained within the directory ./src and its subdirectories:
find ./src -name *.java > sources_list.txt
javac -classpath "${CLASSPATH}" #sources_list.txt
First, find generates sources_list.txt, a file that contains the paths to the Java source files. Next, javac compiles all these sources using the syntax #sources_list.txt.
Here's a code fragment that I use to build an entire project where, as usual, source files are in a deeply nested hierarchy and there are many .jar files that must go into the classpath (requires UNIX utilities):
CLASSPATH=
for x in $(find | grep jar$); do CLASSPATH="$CLASSPATH:$x"; done
SRC=$(find | grep java$)
javac -cp "$CLASSPATH" $SRC