vaj oja
vaj oja

Reputation: 1161

How to resolve this error Caused by: java.lang.ClassNotFoundException

I wrote a console application helloworld.java and ran javac helloworld.java,and then java helloworld. This worked fine.

I then created a package com.abc.project and moved helloworld.java into it(the package import statement is correctly generated which is package com.abc.project;). And then i ran javac helloworld.java this worked also fine and generated the class properly.

However, when I ran java com.abc.project.helloworld from the console, it threw an "class not found" error.

Please can anyone advise what is the problem?

Upvotes: 2

Views: 27984

Answers (2)

Oliver
Oliver

Reputation: 6250

First Please name Class with Capital Letter like HelloWorld.java

If you are in a folder '/myjava' in cmd and your .java files is in this folder then do this in cmd

D:\\myjava\:> javac -d HelloWorld.java

This will create correct package Structure for you Then don't go anywhere from the same location do this

D:\\myjava\:>  java com.abc.project.HelloWorld

It should work fine!!

Upvotes: 0

Keerthivasan
Keerthivasan

Reputation: 12880

Try running

java -cp ABSOLUTE_PATH com.abc.project.helloworld

Where ABSOLUTE_PATH refers to the directory where the class files along with packages are present. say it is bin directory where the class files are generated along with same directory structure as source files

Upvotes: 4

Related Questions