Source File Names

In this tutorial we will create a simple source file and discover what the javac compiler does when the .java source files are compiled. We will then modify the source file with a simple change and then we'll compile the source file again and find out happens. FYI, the technical name of a Java source file is called a compilation unit.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md SourceFileName
C:\Java>cd SourceFileName
C:\Java\helloworld>Notepad SourceFileName.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class SourceFileName {
	public static void main(String args[]) {
		System.out.println("Java source file names do matter.");
	}
}

Now switch back to the command prompt (CMD) and type in javac SourceFileName.java and press Enter.
Type in dir /b and press Enter. This command shows us a listing of all files in the current folder.


C:\Java\SourceFileName>javac SourceFileName.java
C:\Java\SourceFileName>dir /b
SourceFileName.class
SourceFileName.java

As we can see, the javac compiler created a new file called SourceFileName.class. SourceFileName.class contains the Bytecode that is executed by the Java runtime evironment - don't worry too much about this yet. I'll go into detail on how the Java Virtual Machnine (JVM) works in later tutorials. Let's go ahead and run the code by typing in java SourceFileName and press Enter.
When using the java command, be sure to note that we NEVER include .class or .java after the class name.


C:\Java\SourceFileName>java SourceFileName
Java source file names do matter.

We'll make a change to our source file to complicate things a little.
Now lets go back to notepad and change all the class name to all lowercase sourcefilename.


class sourcefilename {
	public static void main(String args[]) {
		System.out.println("Java source file names do matter.");
	}
}

Now switch back to the command prompt (CMD) and type in javac SourceFileName.java and press Enter.
You will notice that no errors are displayed and the cursor simply returns to the next line. Type in dir /b and press Enter.


C:\Java\SourceFileName>javac SourceFileName.java
C:\Java\SourceFileName>dir /b
SourceFileName.class
SourceFileName.java

We still have the same two files, so let's try to execute the class and see what happens.
Type in java SourceFileName and press Enter.


C:\Java\SourceFileName>java SourceFileName
Error: Could not find or load main class SourceFileName

Okay, now type in java sourcefilename and press Enter.


C:\Java\SourceFileName>java sourcefilename
Java source file names do matter.

It is now apparent what has happened. We have a class named sourcefilename in a file named SourceFileName.java. The compiled java bytecode contained in the SourceFileName.class file contains the compiled sourcefilename class. Even though this works just fine, if you don't adhere to the convention of a filename matching the class name, you will create significant issues down the road for both yourself and others programmers as well.


Final thoughts

Always name the file the same as the class to avoid future complications.

Tutorials