aspose file tools*
The moose likes Beginning Java and the fly likes javac: no classpath found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javac: no classpath found" Watch "javac: no classpath found" New topic
Author

javac: no classpath found

sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
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

Joined: Sep 29, 2008
Posts: 12617

Where are you trying to compile the file from (what directory)?
sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
As mentioned above, the directory structure is :

C:\myclasses\here\my\package\HelloWorld.java
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 27, 2010
Posts: 32
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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
C:\Users\sony
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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

Joined: Jun 27, 2010
Posts: 32
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

Joined: Sep 29, 2008
Posts: 12617

You're still not giving it the full filename.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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

Joined: Jun 27, 2010
Posts: 32
I tried this. It doesn't work.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jun 27, 2010
Posts: 32
Where do I set my classpath ?

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

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What is the root of your classes?
sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 27, 2010
Posts: 32
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

Joined: Jan 28, 2008
Posts: 5575

sony vijay wrote:import my.package.*;

this is an import statement; I am asking for the package statement.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 27, 2010
Posts: 32
Thanks a lot everyone for your time and help. This worked.

- Sony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac: no classpath found