Here is the structure of my project:
MyProject
-lib
-mp3agic-0.9.0.jar
-src
-com.company
-Main.java
Inside Main, I import the content of mp3agic:
import com.mpatric.mp3agic.*;
From MyProject, I compile the class using:
javac -cp "lib/*" src/com/company/Main.java
From MyProject/src, I try to run the program with:
java -cp . com/company/Main
and I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mpatric/mp3agic/InvalidDataException
Apparently, the jdk can not find the dependencies from the lib folder when running the class, albeit this problem doesn't occur when compiling.
I guess there's a problem with the syntax of the command, but I just can't find it.
Can you please help me?
EDIT: I solved the problem, moving the lib folder under src and then running:
C:\MyProject\src>javac -cp "lib/*" com/company/Main.java
C:\MyProject\src>java -cp .;"lib/*" com/company/Main
Now, the code compiles and executes without any issue.
HOWEVER, if I leave lib under the project root, this is what happens:
C:\MyProject>javac -cp "lib/*" src/com/company/Main.java
C:\MyProject>java -cp .;"lib/*" src/com/company/Main
Error: Could not find or load main class src.com.company.Main
WHY?
You should use maven or gradle or you'll have to say which console are your using, and which operating system -if you don't know what a console is, you'll first have to google for that before messing with javac directly- But if you insist:
for Linux, OSX, any UNIX-like:
javac -cp ".:lib/mp3agic-0.9.0.jar" src/com/company/Main.java
for Windows:
javac -cp ".;lib/mp3agic-0.9.0.jar" src/com/company/Main.java
To run under windows:
java -cp .;lib\mp4agic-0.9.0.jar com.company.Main
You may need to use slash (/) instead of backslash depending of which console you're using. Or just try "/" or "\" and see what works for you.
If you still get class not found exception, then the issue is not related with classpath or java command but on your code (hard to say, without seeing the code).
java.exe -classpath {path} Main
I think you have to add classpath also and when run the program(not only compile)
Related
I need to include .jar file when running the java class. When I use cmd on my windows, I can successfully run using the following command
java -cp .;MyLib.jar; MyClass
However when I tried to use bash on the same PC, I got error - can't find or load main class. I searched online and some say I needed to use ./* instead of just . for the current directory. So far I tried these commands:
java -cp .:MyLib.jar: MyClass
java -cp ./*:MyLib.jar: MyClass
java -cp ".:MyLib.jar:" MyClass
java -cp "./*:MyLib.jar:" MyClass
only getting "ClassNotFoundException" every time.I know the class exists because if I switch to cmd when it runs fine. I was also able to run the class on bash shell on a mac using the following command
java -cp .:MyLib.jar: MyClass
Then I built a simple class and ran in bash without classpath
java MyTest
So using -classpath caused the issue. How do I include the current directory in bash? Why did it work on bash on a mac? Is it because I was running bash on windows? I am so puzzled by this problem.
And if it helps, I can compile in bash just not able to run the class. javac -cp MyLib.jar MyClass.java compiled the file in bash. I am working under the same directory where MyClass.class is.
Edit: the solution is java -cp ".;MyLib.jar;" MyClass. I thought I posted here for anyone who ran into the same issue as I did. Please see comments for explanation. Thank you torek!
I have 4 Java-Data:
Common.java
Constants.java
KeywordsEditor.java
ExecutionEngine.java (There is here a Main-Method)
I have successful compiled in Command-Line with this Command from Project-Directory (C:\ProjectDemo\src\main\java\ValueInput)
javac -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
I got 4 Data .class in the same Directory. Now i want to run them with this code:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
But i got Error:
Error: ExecutionEngine main class could not be found or loaded
```
I've tried with some same code else:
```
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs\*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs/*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
```
And some more, but they don't work. Can somebody help me?
Update
From your comment, I learnt that you have package ValueInput; mentioned in ExecutionEngine.java. Therefore, you should use the switch -d when compiling:
javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
The option -d . asks the compiler to place the generated class files at the current directory. Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, ValueInput has been created and all the .class files have been placed inside this directory. Learn more about the switches by simply using the command javac
In order to execute ExecutionEngine.class, you can now use the following command:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ValueInput.ExecutionEngine
You can also check this answer for a similar solution.
Side note: You should follow the Java naming conventions. As per the convention, the name of the package should be something like value.input.
Original answer
The root cause of the problem is using only jars with -cp. You missed realizing that your ExecutionEngine.class is not in the jars; rather it is at the current directory which is denoted by a dot (.) which you missed to include in the classpath.
Thus, the correct command will be:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
It doesn't matter where you put . i.e. the current directory e.g. the following will also work for you:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar;." ExecutionEngine
Note for Mac:
The separator used for this purpose in Mac is : instead of ; e.g.
javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
Note for Java-11 onwards:
Java-11 allows launching Single-File Source-Code programs without compiling e.g.
java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
You can learn more about it from this article.
I am running Java program from command line. I am referring to only 1 external jar file. i have added entire path to that jar in classpath. even then i get no class def found error while running program in command line. Program compiles without any error.
I think you complied and run the Java program like this
javac -cp fullyqualifiedPathToExternalJar yourfilepath/filename.java
java -cp fullyqualifiedPathToExternalJar yourfilepath/filename
This is totally wrong. When you compiled and run in this manner program compile successfully but not run. This because you have to follow the syntax of java command Properly.
for compiling its Ok.
javac -cp fullyqualifiedPathToExternalJar yourfilepath/filename.java
To run the program you have to add your file path to the classpath:
java -cp fullyqualifiedPathToExternalJar;yourfilepath filename.java //in windows
java -cp fullyqualifiedPathToExternalJar:yourfilepath filename.java //in linux
The syntax is
javac example.java
java example
with folderpath
javac /home/admin/example.java
java -cp /home/admin example//only class name
Might be the chances of jar's compatibility issue. check yous inter dependent jar versions.
I have been testing the examples (HelloWorld.java) from Sphinx4 with Eclipse, but I would like to compile and run them from the command line.
The application needs 5 .jars to be able to run, I have read that in order to compile a java class with multiple .jars I need to execute the following command (as an example I will show short names):
javac -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld.java
The console does not throw any error messages:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > javac -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld.java
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test >
I think that the compilation succeeded. Now I would like to run the application, I read that in order to do this, I have to execute the command as follows (Using short name example as before):
java -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
This is the message that the console throws:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > java -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld
Error: Could not find or load main class HelloWorld
I don't know what is going on here, I should also say that I do not have a lot of experience using external .jars.
The names of the .jars are:
jsapi.jar
sphinx4.jar
TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar
WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
I appreciate any help you can give me.
You have to include current directory in classpath:
java -cp .:one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
Note the leading .:
From this reference:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Hey I got this problem when running a java program through my cmd.
I use to external libraries : lwjgl.jar and slick-util.jar
when I compile
javac -d bin -sourcepath src -cp ;lib/lwjgl.jar;lib/slick-util.jar src/*.java
everything works fine
but when I try to run it
java -cp bin .;lib/lwjgl.jar;lib/slick-util.jar; Game
I get this error
Error: could not find or load main class .;lib.lwjgl.jar;lib.slick-util.jar;
Replace the space in the run command classpath with a classpath separator
java -cp bin;.;lib/lwjgl.jar;lib/slick-util.jar; Game
^
as an improvement to the run command you could use a classpath wilcards, for example
java -cp ".;bin;lib/*" Game
It is important to use quotes under Windows for this option