Java Basics 1.22

Compiling and Running

Java package statement implies the directory structure where it exists within the project.

Should be unique

  • package com.lynda.javatraining;
  • \src\com\lynda\javatraining\HelloWorld.java

 

When compiling, use javac in the project root:

  • C:\JavaProjects\HelloWorld>javac com\lynda\javatraining\HelloWorld.java
    • HelloWorld.class
    • HelloWorld.java

When running, use package reference and filename without “.java” extension

  • C:\JavaProjects\HellowWorld>java com.lynda.javatraining.HelloWorld

 

 

public class HelloWorld {

 

public static void main(String[] args) {

  • Static: allows class to be called directly?

 

public class HelloWorld {

 

public static void main(String[] args) {

  • Static: allows class to be called directly?

 

 

 

 

Eclipse Development

 

Create Workspace

 

  • Create WORKSPACE folder to contain PROJECTS
  • New, Project....
  • New, Class....(.java file is the class)

  • Window > Prefrences > General > Appearance > Colors and Fonts.. > Text Font
  • Run > Run Configurations... >

 

Import Projects

  • File > Import > General > Existing Project into Workspace
    • Exploded Project (Root Dir)
    • Zipped Project (Archive File)
    • Root Dir, OK
    • Eclipse will autodetect projects
    • Copy projects into workspace
  • Close projects that aren’t in use (Right-Click, Close Project)
    • Projects remain in workspace, just not open

IDE Layout

HutuBBB

Views/Panels

 

 

 

Perspectives

  • Arrangement of Views
  • Custom Perspectives: Window > Save Perspective As....

Command Line

  • Dir to PROJECT\SRC
  • javac Main.java
    • dir = Main.class
    • java Main
  • javac Main.java -d ..\bin
    • \src\Main.class
  • javac Main.java -verbose

 

Note: Access Denied Errors

If Access Denied error occurs, set folder permissions to Everyone and give Full Control access.

 

D:\Eclipse\Java\CommandLine\src>javac Main.java -d D:\Eclipse\Java\CommandLine\bin

Main.java:2: error while writing Main: D:\Eclipse\Java\CommandLine\bin\Main.class (Access is denied

 

public class Main {

     ^

 

 

ClassPath

 

Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when this class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.

The virtual machine searches for and loads classes in this order:

  1. bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional).
  2. extension classes: packages that are in the extension directory of the JRE or JDK, jre/lib/ext/
  3. user-defined packages and libraries

By default only the packages of the JDK standard API and extension packages are accessible without needing to set where to find them. The path for all user-defined packages and libraries must be set in the command-line (or in the Manifest associated with the Jar file containing the classes).

 

Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:

Sometimes you have to check the JAVA_HOME also, if it is pointing towards the right JDK version

set CLASSPATH=D:\myprogram
java org.mypackage.HelloWorld
 

Diagnose

Application programmers may want to find out/debug the current settings under which the application is running:

System.getProperty("java.class.path")