NoClassDefFoundError error while running JAVA console app - java

I created little HelloWorld example and I am having problem running it from command prompt (on Windows). When I attempt to run it by:
java tcpServer from command prompt I get NoClassDefFoundError
I am able to compile it with javac, and class file gets generated.
Somewhere I was reading that I have to put path to my class folder into CLASSPATH environment variable. I've done it and rebooted machine, but I still get the same error.
I also was trying to run it by java -cp c:\MyFolderWhereClassFileIs HelloWorld, it doesn't work.
I've looked into ENV variables and I have following:
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_26;
JRE_HOME:C:\Program Files (x86)\Java\jre6;
CLASSPATH: C:\HelloWorld;
So, how do I run this?
Any ideas how to resolve this? Thanks.
PS. The most annoying thing to me is, if I create java project in eclipse, and create HelloWorld example, then it runs fine...
UPDATE:
Here is the code. It does have package specified.
package test.com;
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
My HelloWorld.java and HelloWorld.class files are here:
C:\workspace\TestApp\src\test\com
One thing I learned so far is that I can't run it from within com folder or test folder. I have to be in src folder class file can be found... but I am still not able to run it... always the same error.

Does your class have a package name? That is, a statement at the top which reads
package <mypkgname>;?
If so, then you have to (A) create the correct directory structure and (B) prefix the class name with the package name in your java command.
For example, if your Java file looks like this:
package hello;
public class HelloWorld {
...
}
Then a basic directory structure would resemble:
src/hello/
src/hello/HelloWorld.java
src/hello/HelloWorld.class
And your bash command would be:
cd src
java hello.HelloWorld
Otherwise, if your class has no package name, it will be placed in the default package. In this case, you simply have to cd to the directory where your class file resides, and run java HelloWorld.

Related

"Could not find or load main class" Error while running java program using cmd prompt [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 2 years ago.
I am running a simple "HelloWorld" Program. I get this error in the command prompt:
Could not find or load main class HelloWorld.
I have set the CLASSPATH and PATH variable in the system. In the cmd prompt, I am running from the directory where I have saved HelloWorld program. I can see the class name and the file name are same and also .class file created in the same directory. What else could be the problem?
My sample program looks like this:
package org.tij.exercises;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
When the Main class is inside a package then you need to run it as follows :
java <packageName>.<MainClassName>
In your case you should run the program as follows :
java org.tij.exercises.HelloWorld
What's your CLASSPATH value?
It may look like this:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
I guess your value does not contain this .;.
So, ADD IT .
When you done , restart CMD
That may works.
For example the file HelloWorld.java is in path: D:\myjavatest\org\yz\test and its package is: org.yz.test.
Now, you're in path D:\myjavatest\ on the CMD line.
Type this to compile it:
javac org/yz/test/HelloWorld.java
Then, type this to run it:
java org.yz.test.HelloWorld
You may get what you want.
I removed bin from the CLASSPATH. I found out that I was executing the java command from the directory where the HelloWorld.java is located, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld
So I moved back to the main directory and executed:
java org.tij.exercises.HelloWorld
and it worked, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld
Hello World!!
Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath and -cp are not used and CLASSPATH is not set, the current directory is used (.), however when running .class files, you need to be in the folder which consist Java package name folders.
So having the .class file in ./target/classes/com/foo/app/App.class, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH variable:
Linux: echo $CLASSPATH
Windows: echo %CLASSPATH%
which has entries separated by :.
See also: How do I run Java .class files?
I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:
C:\Java Example>java -cp . HelloWorld
The -cp option for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.
If you need more information about the java options enter java -? or java -help I think the options are not optional.
I just did some more research I found a website that goes into detail about CLASSPATH. The CLASSPATH must be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:
// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.
When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.
One reason for this error might be
Could not find or load main class <class name>
Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
you can save as Hello.java but,
To Compile : javac Hello.java
This will auto generate A.class file at same location.
Now To Run : java A
Execute your Java program using java -d . HelloWorld command.
This command works when you have declared package.
. represent current directory/.
I had a similar problem when running java on win10
instead of
$ java ./hello
Error: Could not find or load main class ..hello
Run
$ java hello
Hello, World
I was getting the exact same error for forgetting to remove the .class extension when running the JAVA class. So instead of this:
java myClass.class
One should do this:
java myClass
I used IntelliJ to create my .jar, which included some unpacked jars from my libraries. One of these other jars had some signed stuff in the MANIFEST which prevented the .jar from being loaded. No warnings, or anything, just didn't work. Could not find or load main class
Removing the unpacked jar which contained the manifest fixed it.
I faced the same problem and tried everything mentioned here.
The thing was I didn't refresh my project in eclipse after class creation .
And once I refreshed it things worked as expected.
faced the same problem. solved by following these steps
go to directory containing the package 'org.tij.exercises' (e.g: in eclipse it may be your src folder)
use java org.tij.exercises.HelloWorld
For a lot of us, at least for me, I think the class path hierarchy is not intuitive since I'm working inside a directory structure and it feels like that ought to be it.
Java is looking at the name of the class based on it's package path, not just the file path.
It doesn't matter if:
i'm in the local directory ./packagefoo/MainClass, or
a directory up ./packagefoo/, or
one down ./packagefoo/MainClass/foo.
The command "java packagefoo.MainClass" is running off the root %CLASSPATH% which means something significant to Java. Then from there it traverses package names, not path names like us lay coders would expect.
So if my CLASSPATH is set to %CWD%/, then "java packagefoo.MainClass" will work.
If I set the CLASSPATH to %CWD%/packagefoo/ then packagefoo.MainClass can't be found.
Always "java MainClass" means nothing, if it is a member of "package", until I rip out the java code "package packagefoo;" and move the Class File up a directory.
In fact if I change "package packagefoo;" to "package foopackage;" I have to create a subfolder under CLASSPATH/foopackage or foopackage.MainClass stops working again.
To make matters worse, Between PATH, CLASSPATH, JAVAHOME, for Windows, JDeveloper, Oracle Database, and every user name it was installed under, I think a coder trying to just get something up fast ends up brute forcing path variables and structure until something works without understanding what it means.
at least i did.
Create a folder org/tij/exercises and then move HelloWorld.java file. Then run below command
javac -cp . org/tij/exercises/HelloWorld.java
AND
java -cp . org/tij/exercises/HelloWorld
I was facing similar issue but it was due to space character in my file directory where I kept my java class.
Scenario given below along with solution:
public class Sample{
public static void main(String[] args) {
System.out.println("Hello world, Java");
}
}
My Sample.java class was kept at Dir "D:\Java Programs\Sample.java"[NOTE: Package statement not present in java class].
In command prompt, changed directory to "D:\Java Programs\", my programmed compiled but failed to run with error "Could not find or load main class"
After all the possible solutions over SOF(nothing worked), I realized may b space causing me this issue.
Surprisingly removal of folder name space char['Java Programs' -> 'JavaPrograms'], my program executed successfully. Hope it helps

How to access packages?

I wrote a program HelloWorld.java
and stored in a folder(package) named test that test includes hello folder by itself.
and all in my workspace.
I mean this way: d:\workspace\test\hello\HellWorld.java
And I entered the d:\workspace in my path environment, my code:
package test.hello;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("HelloAll");
}
}
When I go to the hello directory at CMD and compile HelloWorld.java everything is fine and done.
but as I use java HelloWorld (in d:\workspace\test\hello) I get exception in thread main error.
Can you help me with this just simple question?
You must use the fully qualified name of your class to run it.
Stand in d:\workspace\
Run:
java test.hello.HelloWorld
in cmd windows go d:\workspace and issue the following cmd
d:>workspace>java test.hello.HelloWorld
cd to d:\workspace
Compile using-
javac -d . HelloWorld.java
The above will create package structure.
Run using-
java test.hello.HelloWorld
You need to use java command from your source directory i.e. d:\workspace as mentioned here:
java test.hello.HelloWorld
The syntax is simple, just go to your source code directory and not the package directory. Use the classname along with full package name.

How to run a simple java class on the command prompt in windows 8

I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.
package gmit;
public class A {
public static void main(String[] args) {
System.out.println("hello");
}
}
In the command prompt I wrote
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>
followed by - javac A.java
java A.class
I tested the class using type A.java
and the text from the class does come up.
What am I doing wrong? Any help would be great.
If it runs in an Eclipse project, but not from command line, you could try this:
C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A
Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.
To compile
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java
To run
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A
Don't append the .class extension while running the java program
Don’t add a .class suffix when using the java command.
Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.
Compilation:
C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java
Executing:
C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A

Running a Java Class in Ubuntu with Virtual Box

Since I'm quite new in Java i have a question. As told on the title , I run an Ubuntu Server on VirtualBox and I have a problem running a very simple class with the use of package.
I give you the code:
package world;
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World")
}
}
Very simple code indeed. After compiling it with javac HelloWorld.java, with no mistakes (ok now what mistakes could possible find),
Running java HelloWorld, gives me the message NoClassDefFoundError
Running java world.HelloWorld returns cannot find or load main class.
I suspect that it has something to do with classpath , but I cannot find an answer.
It's a classpath issue. You can probably check to see what your classpath is by looking at the CLASSPATH environment variable. You can try adding the directory your classfiles are at to the end of this CLASSPATH, but the simplest thing to do is probably the following.
Make sure the HelloWorld.java file is in a directory called world, and you can compile is like:
javac world/HelloWorld.java
This will create a HelloWorld.class file in the world directory. You can then try running
java world.HelloWorld
or
java -classpath . world.HelloWorld
from the same place.
You can also use the -d flag with javac to put the class files in a different place instead of the same place the source (.java files) are.

Running java class from terminal

This question has been asked before but I still cannot figure whats wrong for some reason.
I got a class named NewClass in package syntaxtest in file src. From src path I type :
javac src/syntaxtest/NewClass.java
and the class is compiled and I can see NewClass.class in syntaxtest folder. Now from that same path or even the same folder with NewClass.class, I can't figure out how to run the class from terminal. I have made many different attempts but ether I get
ClassDefNotFound or ClassDefNotFound (wrong name : syntaxtest/NewClass)
Try "java -cp src syntaxtest.NewClass".
That is, if you have a folder "src" which contains the subfolder (package) "syntaxtest" and the class "NewClass" is in "package syntaxtest", then the above command will work.
$ ls src/syntaxtest
NewClass.java
$ cat src/syntaxtest/NewClass.java
package syntaxtest;
public class NewClass {
public static void main(String args[]) {
System.out.println("Hello, World!");
}
}
$ javac src/syntaxtest/NewClass.java
$ java -cp src syntaxtest.NewClass
Hello, World!
I've made the following test:
Created a java file in home/test/blah/TestClass.java
package blah;
public class TestClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Went to directory home/test/
Compiled the file by typing:
javac blah/TestClass.java
Got the file compiled ok.
Ran it by typing:
java blah.TestClass
Got the message "Hello World!" as expected: program runs ok.
Went to directory home/
Tried to run by typing:
java test/blah.TestClass
... and many other combinations of slashes and dots..... nothing worked.... keep getting the same Exception as you:
java.lang.NoClassDefFoundError
So it seems to me that to run a Java class using the command 'java' you really must be in the application's root folder.
I had a similar problem.
I wanted to organize my project using a src and bin folders directly from the Mac finder and use Emacs or some other text editor. I just didn't like eclipse.
You can't execute the classes from another folder, but you can do is compile from another folder into the one you are going to execute.
For example (assuming there is no package), move to the bin folder and run:
$ javac ../src/name.java -d ../bin/
(That compiles from the src folder and outs the .class file directly on bin)

Categories

Resources