Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac: no classpath found

 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I downloaded the latest java jdk and installed it.

I am trying to test a simple HelloWorld.java program.

I have the following directory structure:

C:\myclasses\here\my\package\HelloWorld.java

I set the path variable using the following command in the command line:



I set the class path variable using the following command in the command line:



When I try to compile my java file, I am getting the following error:



Can someone help me fix this. Thanks.

- Sony





 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you trying to compile the file from (what directory)?
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned above, the directory structure is :

C:\myclasses\here\my\package\HelloWorld.java
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Where are you trying to compile the file from (what directory)?

Telling me your directory structure again doesn't actually answer the question...
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. set CLASSPATH=.;C:\myclasses\here

Since the directory structure is C:\myclasses\here\my\package\HelloWorld.java

I am assuming that HelloWorld.java file is wrapped in my\package (package structure).

So, I am trying to compile from the directory "here"

I tried something like this too: javac -classpath .;C:\myclasses\here HelloWorld.java
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To be more specific, David is asking about... (1) what is the current directory when you tried to compile? and (2) the exact command that you used?

Henry
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Users\sony
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:

I am assuming that HelloWorld.java file is wrapped in my\package (package structure).



And you would be wrong. Package structure is for classes -- not java files. When you specify the java file for the compiler, you still need to specify the directory structure.

Henry
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

Now, I shifted two levels above in the directory structure...meaning I am running from the C:\ directory

I tried this:

C:\>javac -classpath .;C:\myclasses\here\my\package HelloWorld.java

It still gives a file not found error
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still not giving it the full filename.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:
C:\>javac -classpath .;C:\myclasses\here\my\package HelloWorld.java

It still gives a file not found error


C:\> cd c:\myclasses\here

C:\myclasses\here\> javac my\package\HelloWorld.java

C:\myclasses\here\> java my.package.HelloWorld


Henry
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this. It doesn't work.

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this. It doesn't work.
Did you set your classpath before executing Henry's suggestion? If so, which part didn't work?
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do I set my classpath ?

In C:\ ? or C:\users\sony\ or C:\myclasses\here ?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the root of your classes?
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My directory structure is C:\myclasses\here\my\package

so, the root of my classes is C:\ , right ?

Do I have to set my path and classpath in C:\ instead of C:\users\sony then ?

In that case, are the following steps right ?

C:\> set PATH=%PATH%;C:\Java\jdk1.6.0_20\bin

C:\> set CLASSPATH=.;C:\myclasses\here\my\package

C:\> cd c:\myclasses\here

C:\myclasses\here\> javac my\package\HelloWorld.java

C:\myclasses\here\> java my.package.HelloWorld

But, am still getting the file not found error. I saved the file using Notepad in Windows 7. So, I guess it attaches a .txt to the filename. Is this the problem ? If am using Windows7 how do I save file with .java extension ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint : note the important question asked by David Newton. in other word, what is your package statement of your class?
What is the root of your classes?
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The root of the java class is :

import my.package.*;


I tried the steps posted in my previous post. It still gives class not found error !
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:import my.package.*;

this is an import statement; I am asking for the package statement.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:
But, am still getting the file not found error. I saved the file using Notepad in Windows 7. So, I guess it attaches a .txt to the filename. Is this the problem ? If am using Windows7 how do I save file with .java extension ?


If the file is not saved with a java extension, then absolutely, this is the problem. You need to save it with the correct name, or the compiler will not be able to find it.

The easiest solution is to bring up a command window, and rename the file using the command line (The GUI is not really good in this regard).

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:Where do I set my classpath ?

In C:\ ? or C:\users\sony\ or C:\myclasses\here ?




Actually, for a beginner, it is probably best to not use a classpath (to start). In fact, the instructions that I gave assumes that a classpath environment has not been set.


Regardless, you should fix the ".java" extension first, as that is likely the main cause to this issue.

Henry
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:Ok.

Now, I shifted two levels above in the directory structure...meaning I am running from the C:\ directory

I tried this:

C:\>javac -classpath .;C:\myclasses\here\my\package HelloWorld.java

It still gives a file not found error

The classpath is only used by Java to find compiled class files. Java does not use it to find Java source files; so including the directory that contains your source code files in the classpath, and then expecting Java to find your source file HelloWorld.java there, is not going to work.

If your class HelloWorld is in a package named my.package, then you need to compile it like this:

C:\myclasses\here> javac my\package\HelloWorld.java

So: Your current directory must be C:\myclasses\here and you must specify the correct path to the source file.

To run it, use this:

C:\> java -classpath C:\myclasses\here my.package.HelloWorld

Here, it doesn't matter what your current directory is. You must add the base directory of your package structure (C:\myclasses\here) to the classpath. Specify the fully-qualified name of the class (my.package.HelloWorld).
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, Thanks a lot for your time and all the previous help !

I implemented the following:

The package statement in HelloWorld.java is: package my.package;

The directory structure is C:\myclasses\here\my\package\HelloWorld.java

I compiled the file as shown below:

C:\myclasses\here>javac my\package\HelloWorld.java

I got this error :
my\package\HelloWorld.java:1: <identifier> expected
package my.package;
1 error

So, I tried removing this package statement and re-compiled. It compiled successfully. Then, I ran this file as shown :

C:\> java -classpath C:\myclasses\here my.package.HelloWorld

I am getting the following exception :

Exception in thread "main" java.lang.NoClassDefFoundError: my/package/HelloWorld
<wrong name: HelloWorld>
-------
Could not find the main class: my.package.HelloWorld. Program will exit


This is the code in my HelloWorld.java file. There is a main method in it. Then, why is it giving the above exception ?



I noticed that HelloWorld.class file is created in C:\myclasses\here>javac my\package\ after I compiled the file.

Thanks Again!

- Sony


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony vijay wrote:I compiled the file as shown below:

C:\myclasses\here>javac my\package\HelloWorld.java

I got this error :
my\package\HelloWorld.java:1: <identifier> expected
package my.package;
1 error


compilation approach is fine . the error indicate that *package my.package;* package is not a valid identifier. i.e, you cant create the package name as *package* , since it is a keyword.

hth
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everyone for your time and help. This worked.

- Sony
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic