The name of the class you define in the source code must be the same as the name of the file.
public class Helloworld {
public static void main( String[] args )
{ System.out.println( "helloworld" );
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main must be declared with keywords "public static void" because it is invoked before any Java objects exist.
If you get an error "non-static method example() cannot be referenced from a static context" it's most likely because
the class is not self instantiated, i.e. Helloworld hw = new Helloworld();
hw.example();
Weird but that's how to follow object oriented logic recursively all the way.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"javac" compiles a .java source code file to .class
(byte code which can run with a Java Run Time Environment)
javac hellworld.java
"java" is the executable binary which will run your object file (.class) in the JRE
java helloworld
(NOTE, we used the class name, not the .class file!)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.jar files are Java Archives
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Most Common Errors:
1a. You do not have a Java Run Time Environment installed.
1b. You have differing versions of the JDK and JRE
"Exception in thread "main" java.lang.UnsupportedClassVersionError: Reverse : Unsupported major.minor version 51.0"
Try using sudo apt-get install openjdk-6-jdk and openjdk-6-jre and see if that forces them to be consistent,
otherwise you may have to use update-java-alternatives
2. You do not have the "path" on your system set to correctly find
to "clobber" or totally erase your class path try a command line:
set CLASSPATH=
3. Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Trying to run the java launcher on the .class file that was created by the compiler.
Do NOT run java on the filename, "java HelloWorldApp.class"
instead, "java HelloWorldApp"
4. Try setting your classpath manually (and include the locations of "helper" class/jar files)
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Each classpath should end with a filename or directory:
-the class path ends with the name of the .zip or .jar file (which contain .class files)
-an unnamed package, the class path ends with the directory that contains the .class files.
-a named package, the class path ends with the directory that contains the "root" package
5. META-INF \ MANIFEST.MF is where files & libraries locations are described, including the
"main entry point" (e.g. location of the main() function)
The manifest file defined in this Jar file has this definition:
Main-Class: org.mypackage.HelloWorld
Class-Path: lib/supportLib.jar
6. Package sealing ensures all classes for that package are archived in the same JAR
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private (class only), package, protected (subclasses too!), public
public static void main()
access special return methodname
static keyword means an object only exists once for a whole class
variables with default sharing are accessible in the package and folder
private restricts it to only the class
protected is like private but allows inheritance
super classes are default
sub classes are created using "Extends"
"base classes" use "abstract"
abstract methods force a sub class to define the method
final method = no override
interface = no code, instead it's used to enforce policy by requiring methods to be coded
methods like prototypes
a CONSTANT = Public Final
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 2012-12-05 johnpfeiffer
// javac Test.java
// java Test
import java.util.Properties;
public class Intro {
public static void main( String[] args )
{
System.out.println( "helloworld" );
Properties system = System.getProperties();
system.list( System.out );
}
}