Problems running a java program from the command line interface - java

I created a java program in Eclipse. When I run the program in Eclipse ("run as -> Java Application") the program runs fine and I have the correct output. However, when I try to run the program in the command line interface I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
Could not find the main class: HelloWorld. Program will exit.
The class file is in directory bin and I try to run it with the command:
java HelloWorld

Since your class is in the package helloworld you should run it like this:
java helloworld.HelloWorld
Also make sure "." is on your classpath.

I try to compile it with the command:
java HelloWorld
TO compile a java program you should use javac command like
javac Helloworld.java

Are you sure that the directory where your classes are is in the classpath? Typically, in your project directory, the "classes" or "lib" directory.
If you are running from that directory, you could try adding ".".
See the -cp parameter of java runtime executable.

Related

Java classpath issue '.' vs. './*'

I am trying to compile and run from the command line a simple java hello world program.
It compiles and runs successfully when I do:
> javac HelloWorld.java
> java HelloWorld
However, when I place a jar-dependency in this hello world program and the corresponding jar in the same directory, I run into a classpath issue.
I can compile with:
> javac -cp ./* HelloWorld.java
But I when I attempt to run while specifying the classpath to the jar, my HelloWorld class is not found.
> java -cp ./* HelloWorld
Error: Could not find or load main class HelloWorld
When classpath is ./*, the jar is found but not the class, and when classpath is ., the class is found but not the jar.
I also tried specifying both, but the main class is not found whenever I use a : in the classpath.
> java -cp "./*:." HelloWorld
Error: Could not find or load main class HelloWorld
How can I specify classpath for java to find both the jar and my class?
In powershell, one solution is to use ; instead of :.
I.e. this works in powershell:
> java -cp "./*;." HelloWorld
hello, world
The idea to try that came from an answer to this question:

How to include an external JAR in a directory within the Java classpath and run the Java class?

I have a Java file that contains an external JAR and I want to run it. It is a simple project: https://github.com/eveningstar33/external-jar and I tested it with Java 8 and also with Java 11. The command to create the class file is javac -cp ./lib/* HelloWorld.java and it's working, but if I try to run the class using this command: java -cp ./lib/*:./ HelloWorld it doesn't work and I get this error message:
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
What should I do to run it? Thank you!
Use java -cp ./lib/* HelloWorld to run the class.
Update:
If the one given above doesn't work, please try with a "" i.e. use java -cp "./lib/*" HelloWorld to run the class.
Another update (tested):
Sorry for posting earlier solutions without testing them myself. Now, I have tested the following to be working:
java -cp .:lib/* HelloWorld

error: could not load or find main class xyz

I have checked my java installations by compiling and running a HelloWorld program which works perfectly fine.
The problem comes when I compile my program with certain jar files which are located in the same directory as my java file. This is what I've done.
javac -cp "A.jar:B.jar" MyProg.java
This generates the class file MyProg.class successfully. Next when I run the following command, it gives this error error: could not load or find main class MyProg
The command is:
java -cp "A.jar:B.jar" MyProg
Next, I even tried next by moving the jars in a folder named lib and issued the following commands:
javac -cp "lib/*" MyProg.jar (works fine;generates a class file)
java -cp "lib/*" MyProg (issues the same error)
I am working on a linux machine. Can some one please resolve the error.
Add the current path to the classpath
java -cp .:A.jar:B.jar MyProg

Java command line compile

I recently finished a programming assignment using Netbeans and Java. When I run the program in Netbeans everything runs perfect. My issue comes when I try and run it from the terminal. I run javac with all my .java files and it produces a .class file for each one. Then when I try to run the executable it errors out on me.
ls
Board.java KenKenPuzzleSolver.java
Solver.java Cage.java
Size.java Values.java
javac KenKenPuzzleSolver.java Board.java Cage.java Size.java Solver.java Values.java
ls
Board.class Size.class
Board.java Size.java
Cage.class Solver.class
Cage.java Solver.java
KenKenPuzzleSolver.class Values.class
KenKenPuzzleSolver.java Values.java
java KenKenPuzzleSolver
Exception in thread "main" java.lang.NoClassDefFoundError: KenKenPuzzleSolver (wrong name: kenkenpuzzlesolver/KenKenPuzzleSolver)
Also, when the program is run from the terminal it should take in an input file and the name of an output file i.e. "java KenKenPuzzleSolver input.txt output.txt", and I am not quite sure how to do this.
Why don't you generate an executable with netbeans? You don't have to compile your code every time.
input.txt and output.txt are called arguments. They will be stored in the String[] args array in you main function.
You did not provide the classpath parameter.
Use javac -cp YOURCLASSPATH KenKenPuzzleSolver
The class that you are running should be public and should have the class name same as your file name. It seems like your file name is "KenKenPuzzleSolver" but your class name seems to be "KenkenPuzzleSolver" ( Observe the lowercase ken ). correct that and recompile and re run.
You must have messed with cases, from error its obvious instead of KenKenPuzzleSolver you have named your class as kenkenpuzzlesolverin KenKenPuzzleSolver.java.

Problems compiling and running Java app with Bluecove (NoClassDefFoundError)

I have this app that uses bluetooth, so I need both, bluecove and bluecove-gpl packages, when I run it in NetBeans I have no problem at all, and works perfectly fine. But I still can't compile and run from the command line (Ubuntu 11.04).
I'm using this line for compilation:
$ javac -Xlint:unchecked -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client.java
And it doesn't return errors and it generates a .class file
Then I try to run the .class file like this:
java -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
But it returns a NoClassDefFoundError.
Could not find the main class: SPPClient.
Why is this happening?
You probably need to add your current directory (or whatever directory your class files reside in) to the class path.
Try something like
java -classpath .:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
or
java -classpath bin:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
You need to have the main class definition in the manifest file:
Main-Class: classname

Categories

Resources