Sample Java classpath, import and package statements.

Getting your classpath, packages and import statements to all work together 
seamlessly can be a challenge, so I put together this sample that is quick 
and easy to modify for your particular situation. 

The two Java programs don't do anything besides responding to a roll call 
when you run them. Their purpose is just to verify that the package and 
import statements work together with the parameters used to compile and 
run them. 

The first paragraph below shows the contents of the three files needed: 
a bash file to compile and run the programs, along with the content of 
each program. 

The second paragraph contains all the commands needed in a terminal 
session to set this up, using Linux (Ubuntu).


#!/bin/bash         

cd /rpv

JAVAC1=/xyz/.../bin/javac
JAVA1 =/xyz/.../bin/java

JPA=~/JAVASRC
CPA=~/JAVACLA

PPA=flavored.coffee.
PPADIR=flavored/coffee
FIL=Latte
# For the 2nd run, comment out the next three lines:
PPA=flavored.
PPADIR=flavored
FIL=Beverage

OPT1=

if ls $CPA/$PPADIR/*.class > /dev/null 2>&1;
then
  echo ".class file(s) exist(s)"
  rm $CPA/$PPADIR/*.class
  echo ".class file(s) deleted."
else
  echo ".class file(s) exist(s) NOT"
fi

$JAVAC1 -cp $CPA: $OPT1 -d $CPA $JPA/$FIL.java  

if ls $CPA/$PPADIR/*.class > /dev/null 2>&1;
then
  echo "N-E-W class file, will run, watch for runtime exceptions below:"
  sudo $JAVA1 -cp $CPA: $PPA$FIL $FIL
else
  echo ".class file(s) exist(s) NOT"
fi

echo Done, the class file, if any, has completed! 
echo Watch for runtime exceptions above.
# Script file ends here. Do not include anything below in the script file.

Contents of file "Beverage.java" in directory ~/JAVASRC:
package flavored;

public class Beverage { // class will be put in: ~/JAVACLA/flavored
  public static void main(String [] args) { 
    System.out.println("This is: " + args[0]); }
}

Contents of file "Latte.java" in directory ~/JAVASRC:
package flavored.coffee;
import flavored.*;

public class Latte extends Beverage { 
  // class will be put in: ~/JAVACLA/flavored/coffee
  public static void main(String [] args) { 
    System.out.println("This is: " + args[0]); }
}

sudo mkdir rpv (must exist, but won't be used) 
cd ~ (to get to your home directory) 
mkdir JAVASRC 
mkdir JAVACLA 
mkdir JAVAEXE 
cd JAVACLA 
mkdir flavored 
cd flavored 
mkdir coffee 
cd ~/JAVASRC 
gedit Bevarage.java (copy and paste from paragraph above) 
gedit Latte.java 
cd ~/JAVAEXE 
gedit JAVAX (copy and paste bash script above)
(Also replace "/xyz/..." with the path to java on your system) 
chmod 755 JAVAX (to make it executable) 
./JAVAX (should create and run Beverage.class in ~/JAVACLA/flavored/) 
gedit JAVAX (comment out three lines as indicated. Use #, not //) 
./JAVAX (should create and run Latte.class in ~/JAVACLA/flavored/coffee/);
 

Valid XHTML 1.0 Transitional Valid CSS!
 


MCP icon
MCTS icon