Error when try to run my project in cmd - java

I have a structure code like this :
I want to run my program using mcd using javac, like this : javac ListenerZipFile.java. The result like :
Why i can't run my program?

you are using a package without telling javac where it is located (jnotify for example).
you'd have to use it like:
javac -classpath "path/to/jnotify-0.94.jar" test.java

There are 2 problems here.
Incorrect directory location for compiling packed classes.
classpath no set correctly.
Consider you have, source_dir = D:\~\~\src, jar_location = D:\~\~\lib and package is com.example then your steps to compile are:
cd to $source_dir
$source_dir> set classpath=.;jar_location
$source_dir> javac com\example\Examples1.java or $source_dir> javac com\example\*.java
As per path shared, command to compile should be :
cd C:\ListenerZipfile\src
javac -cp .;C:\ListenerZipfile\lib\*.jar com\sigma\main\ListenerZipFile.java
Command to run java program with above path:
java -cp .;C:\ListenerZipfile\lib\*.jar com.sigma.main.ListenerZipFile

if you are using eclipse (from the screen shot i think you do) then
right click on your project->Properties->Java Build Path, click on Libraries tab->click on "Add External JARs" button-> pick your jar and click OK to close all windows.
This should solve your problem, if your problem is jar dependencies

Related

Class not found when running program from command line

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)

No class def found error even though the jar is added to classpath

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.

How to compile and run HelloWorld.java example from Sphinx4 without and IDE in Linux?

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.

Cannot find class in classpath

I've been reading up on this error but am now confused. I'm trying to run my java test file from the win7 cmd line but am getting that dreaded error cannot find class in classpath.
The script runs fine as a testNG file inside the Eclipse IDE but not from the cmd line.
My classpath dump:
C:\lib\selenium-java-2.37.0.jar;
C:\lib\selenium-server-standalone-2.37.0.jar;
C:\EclipseIDE\ProteinBar\bin;
C:\EclipseIDE\ProteinBar\src\com\proteinbar\staging;
C:\TestNG;
My cmd line test string:
java -cp C:\lib\*;C:\EclipseIDE\ProteinBar\bin org.testng.TestNG DeleteBillingAddress.xml
Thanks for any help...
it doesn't appear that the testng jars are on your classpath. I'm guessing they live under c:/testng so you'll want to add something to your -cp reflecting that.
You need to specify the jars individually, e.g.,
java -cp C:\lib\selenium-java-2.37.0.jar;C:\lib\selenium-server-standalone-2.37.0.jar;...

How do I run java program with multiple classes from cmd?

At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)

Categories