*
The moose likes Beginning Java and the fly likes Javac: File Not 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: File Not Found" Watch "Javac: File Not Found" New topic
Author

Javac: File Not Found

Travis Rademacher
Greenhorn

Joined: Jul 05, 2010
Posts: 6
I wrote down CLASSPATH and PATH in my system variables already.
The CLASSPATH's value is %classpath%;. And the PATH's value is %path%;C:\Program Files\Java\jdk1.6.0_20\bin

I'm not sure if there are any errors in that above, so I just included it.
After this, created my source file, saved it in C:\Program Files\Java\jdk1.6.0_20\bin as MyFirstApp.java. So I open command prompt, enter: javac MyFirstApp.java

This is what comes up:

javac: file not found: MyFirstApp.java
Usage: javac <options> <source files>
use -help for a list of possible options

Help?


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

After this, created my source file, saved it in C:\Program Files\Java\jdk1.6.0_20\bin as MyFirstApp.java. So I open command prompt, enter: javac MyFirstApp.java

First of all, saving sources in your JDK installation bin directory is not recommended. You should put put your sources somewhere else, like "C:\Java"

javac: file not found: MyFirstApp.java

javac does not use the CLASSPATH to find the file you pass on the command line. You have to tell it where the file is. In your case, the file is in C:\Program Files\Java\jdk1.6.0_20\bin, so either move to this directory (cd C:\Program Files\Java\jdk1.6.0_20\bin), or compile the class using its full path (javac C:\Program Files\Java\jdk1.6.0_20\bin\MyFirstApp.java)

[My Blog]
All roads lead to JavaRanch
Travis Rademacher
Greenhorn

Joined: Jul 05, 2010
Posts: 6
Thank you so much. I have another problem. I compiled the source file fine. Now I tried to run the class file. In command prompt, I typed: java c:\java\myfirstapp.class

It gave me a message:
Exception in the thread "main" java.lang.NoClassDefFoundError: c:\java\myfirstapp/class

Caused by: Java.lang.ClassNotFoundException: C:\java\myfirstapp.class
at:

And then a bunch of stuff


This is my source file:

public class MyFirstApp {

public static void main (String[] args) {
System.out.print("JAVA!!!"); }
}


Is it an error in my programming or what?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

ClassNotFoundException tells you that the JVM cannot find your class. You need to set the CLASSPATH properly to run java programs. There are different ways to do it, one of them being to use the "-cp" flag on the command line. For example, you can go to C:\java (cd c:\java) and tell the JVM to look for class files in the current directory, like this : java -cp . MyFirstApp. Be careful that class names are case sensitive, and that you don't need to pass the extension (.class).
Alpesh Padra
Ranch Hand

Joined: Jan 10, 2010
Posts: 41
Use java -classpath <DirectoryOfClass> classname.

Hope this will resolve your problem.
Travis Rademacher
Greenhorn

Joined: Jul 05, 2010
Posts: 6
What I did was:

java -cp <c:\java> MyFirstApp

It told me access is denied. I am system administrator so this confuses me.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What are these brackets for ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I see. I guess you left them after seeing the help :


Remove them.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Have a look at Sun's Hello World tutorial, it explains you step by step how to write, compile and run your first Java application and it also describes common problems and solutions.

I see you're not familiar with how the Windows command prompt works. It's a good idea to familiarize yourself with the concepts of the Windows command prompt first, so that it's easier to understand what you're doing and what those commands mean.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Travis Rademacher
Greenhorn

Joined: Jul 05, 2010
Posts: 6
Thank you christophe. My code works fine now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javac: File Not Found