Java

One of the ideas behind Java is that Java programs run on different microprocessor architectures without the need of a recompilation. Write the program once and run it everywhere, this is the slogan of Java! This is accomplished by compiling the Java program into Java byte code that is neutral of any microprocessor architecture.

Additionally java programs make use of java libraries and therefore the programs run on different operation systems as well. Many time no installation is required.

To run the Java byte code a virtual machine (JVM) must be installed on your computer. There are java runtime environments JRE and java development kits JDK (that include JRE). If you like to develop java then JDK is required otherwise JRE is enough.

Different java implementations exist. The original from sun is the most complete and some packages depend just on that (clean gentoo packages should depend on the virtual package java and give therefore the freedom to choose the java implementation). However it is a pain to install and keep it updated since sun wants the users to manually download it. An other pain is that this can just be done easily for the newest versions. To get an older version where your package probably depends to, you need to register as developer with user name and password and login manually. Since those versions get updated frequently and Gentoo Linux is a living distribution it becomes really a pain. To get java from sun and struggle with their licenses emerge -v sun-jdk

A license painless version of java is icedtea, There is a binary and a source version to be chosen from. So emerge icedtea or emerge icedtea-bin

There is also a java useflag to be set. See if you have such a VM and the path to it:

java-config -c

/opt/icedtea-bin-6.1.11.4/bin/javac

if not

java-config -L

The following VMs are available for generation-2:

*)IcedTea JDK 6.1.11.4 [icedtea-bin-6]

java-config --set-system-vm=icedtea-bin-6

env-update && source /etc/profile

The gentoo way is to use eselect for it

eselect java-vm list

and to set:

eselect java-vm set system 2

Finally a good way to see what is used

java -version

There are different implementations and editions of java. javaSe is the standard edition and might be the one to go. Due to licenses, the original VM (Virtual Machine) from sun is a bit a hassle to install. The emerge commando can not install it automatically, since sun wants that you do it manually and click on the yes I accept the license button. Therefore the icedtea version is often used as an alternative with less hassle. However I takes the documentation from sun where you run in the same situation. Luckily the ebuilds fail with an instruction where to get the files from sun and where to store them (usually /etc/portage/distfiles). After having done this procedure of clicking yes I want, yes I accept, yes I'm sure, yes I'm still sure, the ebuilds run automatically with success. There is a doc useflag that can be cleared to avoid this sun hassle, but it removes all other documentation, therefore the doc useflag has to be just removed for java. See the useflag section to find out how to define useflags for single per packages.

Java hello world program

The tutorials from https://docs.oracle.com/javase/tutorial/are a good starting point. Note java is case sensitive and java is not an interpreter language, so copy the following in a console and you have the first source code:

cat > hello.java << "EOF"

#!/usr/bin/java

class HelloWorld {

public static void main (String args[]){

System.out.println("Hello World!");

}

}

EOF

and then compile it into java byte code unsing javac the java compiler

javac<name of your java program>.java

As a result you get the the byte code file <name of your java program>.class that you can run with java the java virtual machine:

java<name of your java program>

Java hello world applet

Java applets are java programs running inside a web browser.

Note

Note the difference between java applets and java scripts. Java applets are compiled and contain byte code versus java scripts are an own language that differs from java. Java scripts is typically source code embedded in html pages and is interpreted and run by the browser.

Java and html are therefore separated in two different files. The html file contains a link to the compiled java file. Nothing has to be configured for a web design using java since the class files are handled as images, obviously just the link has to match. Copy the following in a console and you have the hello.html page:

cat > hello.html << "EOF"

<html>

<head>

<title>Hello Applet</title>

</head>

<body>

<p> my applet says:<br>

<applet code="HelloWorldApplet.class" width=150 height=25>

</applet>

</body>

</html>

EOF

And here the java source,

cat > HelloWorldApplet.java << "EOF"

import java.awt.Graphics;

public class HelloWorldApplet extends java.applet.Applet {

public void paint(Graphics g) {

g.drawString("Hello world!", 5, 15);

}

}

EOF

that has to be compiled using javac to HelloWorldApplet.class:

javac HelloWorldApplet.java

Html browsers as konqueror and firefox contain a cache that can fool you when debugging and making modifications to your applet, since they pop up the previous versions. An alternative is using appletviewer<my html containing the applet>.html

Java programs might also have the extension jar. This is a java archive and contains multiple files inside. To run a jar file tape java -jar <name of the jar file> . The jar files can also be observed and extracted with a archive manager.

There is javadoc a tool that creates documentation out of java source code (similar to doxygen)

Netbeans

Netbeans is an IDE to program java (and some other language) since it is entirely written in java it can run everywhere java is installed. As can be seen from http://netbeans.org/downloads/index.html there are different bundles that support different features.

A deeper look inside java

Variables belonging to a class are called fields.

Interfaces in the java language define a set of methods declarations without their implementation. If a class wants to use this interface this is done with the keyword implements, the class gets those empty method declarations, but must also then implement code to those method names. Interfaces can be seen as intention to have and the complier checks it this is the case.

The standard API can be found at https://docs.oracle.com/javase/7/docs/api/index.html

Variables declared as static in classes are class variables that exist just once among all inherited objects, this means all objects read and write the same value.

Overloading is adding a method with the same name but a different parameter list, troughs both can be used, while overriding replaces an method by a method with the same parameter list. Overriding is often used when a inherited class wants a different implementation than its super class. Often the method of the super class needs just to be extended with some more to do. The method that overrides the super class method can make a call to super to have the method in the superclass running and then execute the additional stuff.

The < is used for generics, using generics allows to detect more errors during compilation time and therefore reduced the number of run time errors.

Packages allow you to bundle different java code together and put them in a namespace. The reversed Internet domain name is commonly used to get unique package names.

Java Foundation Classes (JFC) has Swing to allow gui programming.


Linurs startpage