user2328847
user2328847

Reputation: 11

"javac: file not found:" when compiling file on Mac

I am trying to learn Java on MacBook Pro, but am not even sure how to compile or run a program and keep getting the following in Terminal:

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

when I enter my first code for Example.java saved in Text Edit. The code I entered into Text Edit is

/* 
This is a simple Java program. 
Call this file Example.java. 
*/ 
class Example { 
// A Java program begins with a  call to main (). 
public static void main (String args[]) { 
System.out.println ("Java drives the web."); 
} 
}  
javac Example.java  
java Example 

I have found info saying to set up a class path, but am not sure how to do this. Please keep everything as simple as possible.

Upvotes: 0

Views: 13465

Answers (3)

Dlewid
Dlewid

Reputation: 11

If none of these solutions are working and you are using Monterey 12.3.1, try going to settings > system & privacy find your terminal and files and folders and make sure to unlock them for access.

Upvotes: 1

rolfl
rolfl

Reputation: 17707

Make sure your Java file is saved as Example.java (case matters), and that it is in the current directory (.).

You should be able to compile it then with javac Example.java as per your example.

But, to make sure you can compile with (these are basically the 'default' options):

javac -sourcepath .  -d . Example.java

When you run the class with:

java -cp . Example

Upvotes: 3

Alex Kalicki
Alex Kalicki

Reputation: 1533

The javac command only searches for files within the default java PATH and the current directory. Make sure that you've already navigated to the directory that holds Example.java using the cd command before trying to compile.

For example, if the file's located on the Desktop:

cd ~/Desktop/
javac Example.java
java Example

Upvotes: 0

Related Questions