[last updated: 2019-05-28]
go to: Ham Radio
go to: SDR Study
go to: Java
-----
-----
Editing Java programs:
- Any text editor can be used to create a Java program.
Save the program with a .java extension.
This now is a Java source file
- Source files each define just one class.
The name of the source file is the same as the name of the class being defined.
Both names start with a capital letter and use CamelCase format.
- Your source file will be located in a directory, and a directory that contains source files is also called a Package (see: (go to:) Package/Directory Structure.)
Package names should start with a lower-case letter.
-----
Compiling Java programs:
- Suppose you have a source file named Prog1.java.
In that file you should have defined a class named Prog1.
Then using the command line/terminal, when you are in the directory where the source file is located,
compile it with this command:
$ javac Prog1.java
this will create a file in the same directory that is named Prog1.class.
Class names should be capitalized.
- Follow this same procedure regardless of whether your source file has a package declaration.
-----
Executing Java programs:
- Suppose your source file did NOT have a package name declaration as the first line in the file.
In that case you can execute it (the compiled Class file) from the command line, while you are located in its directory, with this:
$ java Prog1 (NOT including the .Class extension)
(This only works if your CLASSPATH includes the default directory that you're in. If however it has been cleared and does not include your default directory, this command will not work)
- However instead suppose that your class file is located in a directory (package) named dir1
Then the first line of your source file must be:
package dir1;
Further, now to execute the class file (once re-compiled),
you must move to the directory above dir1, and execute the file with this:
$ java dir1.Prog1
or this:
$ java dir1/Prog1
- And if more deeply nested:
suppose your class file is located in a directory/package named: dir2/dir1
then the first line of your source file must be:
package dir2.dir1;
and to execute the (re-compiled) class file,
you must move to the directory above dir2/dir1, and execute the file with this:
$ java dir2.dir1.Prog1
or this:
$ java dir2/dir1/Prog1
- In summary, if you have a package statement in your source file:
- Always execute with:
$ java [classFilename]
where classFilename includes all directories specified in the package statement,
and does NOT include the .class extension
- If you have NOT set your CLASSPATH variable,
you must be located in the directory above the path specified in your package statement
- If you set your CLASSPATH variable to include the directory above the path specified in your package statement, then you can execute from anywhere.
-----
Compiling and Executing after setting CLASSPATH:
- Editing/defining your (go to:) CLASSPATH system variable
allows compiling and executing from anywhere????
- ...
-----
.
.
.
eof