[Navigation Bar]  
 
 

    

[OpenSUSE powered]
[BUSH powered]
[vi powered]
[XML] [RSS]

 You're a C programmer who got a Linux Java project dumped in your lap. What do you do now?

Basics Steps to Working with Java

Java is a language designed for platform independence. It has its own development tools, file types and object code. The following steps build and execute a typical Java program:

  1. Compile source code programs with javac (or gcj).
  2. Run the application.
    1. Run applications with java (or gij).
    2. Run web page applets with appletviewer (or a web browser).

Java uses the following filetypes:

  • Source files have a .java suffix.
  • Byte code object files have a .class suffix.

When you compile one .java file, it can create more than one .class file.

GCC Java can also create regular native Linux files.

 

Java Terminology

Java has many acronyms and terms. Here's a few you may encounter:

  • ANT - an XML-based "make" program for Java
  • Applet - a Java program that runs in a web page, interfacing with the web browser showing the page.
  • Application - a Java program that runs without a web browser
  • AWT - Abstract Windowing Toolkit - the function and class libraries bundled with Java for creating mouse-and-windows applications. If you draw a square or handle mouse clicks, you're using it.
  • J2EE - Java 2 Platform Enterprise Edition - Java with JSP, enterprise JavaBeans, etc. included for developing huge Java programs. Often bundled with special server software.
  • J2SE - Java 2 Platform Standard Edition - Java with plain JavaBeans, no JSP, etc. for developing typical Java programs. Sometimes called "plain old Java", this is what most people refer to when developing with Java.
  • JAR - a Java Archive file
  • Java 2 - Java version 1.2 (or newer)
  • JDK - Java Development Kit - development tools for a Java system such as the compiler and the interpreter. The complete J2SE as opposed to JRE.
  • JNI - Java Native Interface - running Java software from a native program. e. g., a C program calling a Java function
  • JRE - Java Runtime Environment - stuff needed to run Java programs but without the development tools to build new programs. A subset of J2SE for non-programmers.
  • JSP - Java Server Pages - a PHP-like way of embedding Java into HTML web pages using XML
  • JVM - Java Virtual Machine - the environment that a Java program runs in that isolates Java from the host hardware.
  • SDK - Software Development Kit - older term for JDK
  • Servlet - a Java program that runs on a web server, as opposed to an applet
  • Tomcat - an Apache program that runs Java servlets. That is, it's a server daemon that waits for network connections, runs Java programs and return the results back over the connection. It serves program output the way a web server serves web pages.
 

Which Java Product Are You Using?

You may have more than one instance of Java on your computer (e.g. IBM Java, SUN Java and/or GCC Java, etc.). Each has its own libraries, compilers and interpreters. Mixing different versions can have unpredictable results.

$ /usr/bin/javac 2>&1 | head -2
Eclipse Java Compiler v_686_R32x, 3.2.2 release
Copyright IBM Corp 2000, 2006. All rights reserved.

$ /usr/java/jdk1.5.0_09/bin/javac 2>&1 | head -2 # SUN Java
Usage: javac  
where possible options include:

$ gcj --version
gcj (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)
Copyright (C) 2006 Free Software Foundation, Inc.

Some environment variables affect how things run.

  • CLASSPATH - places to look for and load classes (.class directories or .jar file names)
  • JAVA_HOME - the home directory for the Java tools (useful if you have more than one version of Java)
 

Windows: Set or change environment variables by right-clicking on your "My Computer" icon and select the Advanced/"Environment Variables" button. It's strongly recommended that you add the Java bin directory to your path and JAVA_HOME (the location of your jdk directory) to your environment variables. Typing "java -version" at a new command prompt window should work.

 

GCC Java on Linux

Java is one of the GCC languages. It is available if it is enabled and included by your Linux distribution.

GCC Java can create executable machine code or portable Java byte code. To use it in the typical way, use -C to create .class files.

GCC Java respects the CLASSPATH environment variable. Additional class paths can be given with "--classpath=" option (-cp is supported with gij).

gij works the same way and has similar options to Sun's "java" command. gij may not be available on all platforms.

$ gcj -C -Wall Example2.java
$ gij Example2

Compile into executable machine code by leaving off the "-C".

Basic gcj options:

  • --classpath="" - add additional library paths (.class files)
  • -I - additional library paths (the GCC way)
  • -Wall - enable all warnings

Basic gij Options:

  • -classpath (or -cp) - add additional library paths

GCC Java Utilities:

  • jv-scan - summarize a Java source file
  • jcf-dump - summarize a Java .class file (like Sun's javap)
  • gcjh - create stubs or header files for Java classes
  • gjnih - create JNI header files (like gcjh -jni)
 

Sun JDK on Linux

Sun Java comes in two parts: the run-time environment (what you need to run Java programs) and the software developer's tooklit (SDK, what you need to compile Java programs). Check to make sure that your version of Linux has both the environment and the developer's tools installed.

Use the JAVA_HOME variable to indicate where the bin directory with javac is located. (This may already be configured by your Linux distribution.)

$ export JAVA_HOME="/usr/java/jdk1.5.0_09/"

If you need to add the man pages, install the Sun Manual Pages (JDK Java Developer's Toolkit ) and use MANPATH to indicate where they are:

$ export MANPATH="$MANPATH:/usr/java/jdk1.5.0_09/man"
$ man java

Compiling Java Applications (Sun Java Developer's Toolkit (JDK) ):

$ /usr/java/jdk1.5.0_09/bin/javac -cp . Example2.java
$ /usr/java/jdk1.5.0_09/bin/java -cp . Example2

The exact location depends on your version of Linux.

Basic javac options:

  • -classpath (or -cp) - add additional library paths (.class or .jar files)
  • -d - destination for object files
  • -g - full debugging messages (primarily) for jdb
  • -sourcepath - additional include paths (.java files)
  • -Xlint - enable all warnings

Basic java options:

  • -classpath (or -cp) - add additional library paths (.class files)

Other Sun Java Utilities

  • javap - summarize a Java .class file
  • jdb - an interactive debugger file
 

Compiling Java Applets (Sun Java Developer's Toolkit (JDK) )

$ /usr/java/jdk1.5.0_09/bin/javac -cp . AppletSimple.java
$ /usr/java/jdk1.5.0_09/bin/appletviewer AppletSimple.html

To use the applet viewer program, make sure you run the web page not the Java files. The web page gives the context for the applet (including the size of the display area and any parameters to the applet). Running the Java class by mistake will give you an error about a missing default parameters.

Running the applet in a browser, the browser caches the Applet and must be quit and restarted to see the changes to the Applet.

Don't assume the applet will run the same in a web browser and the applet viewer: for example, if you have a background thread, the web browser may not call the Run() function as often as the applet viewer does. Different versions of Java may behave differently.

For most non-trivial applications, you'll need a background thread to do work. However, the graphics are separated from the reset of the program. This means there can be delays in updating the graphics. If you are not updating the entire window (only parts), you need to keep track of what was displayed at the time of the last redrawing. For example, in an animated game, you need to erase a moving object where it was last drawn, knowing that drawing may not have occured for several animation frames.

 

Java Applications

Create an application by extending the AWT frame class.

 

File names, Packages and Periods

Packages, in Java, are a way to specify subdirectories. This affects how you run an application or applet. For example, "java a.b.c" is looking for a file "a/b/c.class" containing a "a.b.c" class to execute. The command, the current directory and path to the class and the class name within the file must all be correct in order to run "a.b.c".

Java will report a "java.lang.NoClassDefFoundError / wrong name" error if you try to run a class using the wrong name. (That is, the class file was found but a class with the exact name wasn't found in the file.

You can also get a java.lang.NoClassDefFoundError if you try running a class from the wrong directory. You may have to move up (or down) a directory to run a class that is within a package, or change your class path.

 

Jars

A ".jar" file is a Java ARchive. This is a compressed file containing other files, like a ".zip" file, ".tgz" file or other types of archives. They are constructed and managed using the "jar" command.

Jar files are often used like traditional libraries files for languages like C, created a collection of many .class files. The Java interpreter automatically searches a .jar file in the CLASSPATH...but you must include the jar file name, not just the directory containing the jar file. Think of the jar file as containing a directory with classes.

Note: the jar command is not very smart. e.g. it doesn't follow symbolic links.

 

Making Projects (ant)

To build projects with multiple files, many Java developers use a "make" program, written in Java, called "ant" (http://ant.apache.org/). Part of the Apache web server project, ant is platform-independent and uses XML configuration files to describe a project's files depend on one another.

There is an ANT_HOME variable.

For simple projects, this is just a list of <target> tags.

<project name="MyProject" default="dist" basedir=".">
    <description>
        This is a simple build file.
    </description>
    <target name="clean"/>....</target>
    <target name="main" depends="secondary"/>....</target>
    <target name="secondary" depends="sub1,sub2"/>....</target>
</project>

There are many additional tags for creating or deleting files, setting default directories, checking for different conditions and so forth.

To use ant, name your build XML file "build.xml" and run "ant" in the same directory.

You could use the traditional Linux "make" project building command, if you prefer.

 

Example: Installing and Using Jogl on OpenSuSE 10.2

First, chose the version of Java to use. Since Jogl is developed by Sun, I used Sun Java.

Although Jogl is available in YAST (SuSE's setup tool), SuSE's version is missing files. So I went to the Jogl web site and unzipped a pre-built version of Jogl in my home directory.

With Jogl in my home directory, I needed to include the two jar files in my class path when compiling programs using Jogl. Don't include the directory of the broken version of jogl that SuSE provides.

$ javac -Xlint -cp "/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/jogl.jar:/h
ome/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/gluegen-rt.jar" someprogram.java

The Jogl demos required the ant build tool. ant was already installed, but SuSE had some broken links. ant is not very friendly: I had to run ant as "ant --execdebug" (an option not listed with ant --help) to find the directories ant was looking for and added symbolic links.

Also, ant will not be able to find jogl unless the jogl.jar path is changed in "build.xml".

Also, ant will not be able to find jogl unless the jogl.jar path is changed in "build.xml".

<!-- property name="jogl.jar"            value="../../jogl/build/jogl.jar" / -->
<property name="jogl.jar"            value="/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/jogl.jar" />

Since Jogl uses shared libraries, these must be available to the java interpreter or else you'll get a linking error. The simplest way is to use the LD_LIBRARY_PATH variable.

$ echo "$LD_LIBRARY_PATH"

$ export LD_LIBRARY_PATH="/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib"

If you are a system administrator, you should be able to put the jogl directory in a shared place and use "ldconfig" to add this directory to the system defaults.

The Java examples are copied to the "build/classes" directory. Add this directory to your class path when running java.

$ java -cp "/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/jogl.jar:/home/ke
n/jogl-1.1.1-pre-20070812-linux-amd64/lib/gluegen-rt.jar:classes" demos.gears.G
ears

The gears example ran. The other examples threw exceptions.

I created a Makefile to automate the jogl build process. I tested it with the Gears demo by renaming the demo, removing the package command and building it in another directory.

all:
        javac -Xlint -cp "/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/jogl.jar:/ho
me/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/gluegen-rt.jar" Gears.java

run:
        export LD_LIBRARY_PATH="/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib"; java
 -cp "/home/ken/jogl-1.1.1-pre-20070812-linux-amd64/lib/jogl.jar:/home/ken/jogl-1.1.1-pr
e-20070812-linux-amd64/lib/gluegen-rt.jar:." Gears

clean:
        rm *.class

Read the Sun's Java web site documentation for more information.

Read More:  Return to the Front Page --> 

 
     

« Truth Humility Communication Nobility Freedom Purity Excellence Right Support Courage Compassion Quality Honesty Trust Cooperation Challenge Education »
PegaSoft Canada - A Linux Association Since 1994