Java
JDK
- select version of JAVA to use (Oracle or OpenJDK)
$ sudo update-alternatives --config java
- oracle.com: Java SE Downloads
- download
jdk-8u[xxx]-linux-x64.rpm
at Java SE Development Kit 8u71
- install to
/usr/java/latest
$ sudo rpm -i jdk-8u[xxx]-linux-x64.rpm
- install openjdk sources
$ sudo dnf install java-1.8.0-openjdk-src.x86_64
- add environment variable (
/etc/environment
or~/.profile
) - `JDK_HOME=”/usr/lib/jvm/java-openjdk”
OpenJFX
- get sources using
mercurial
$ hg clone http://hg.openjdk.java.net/openjfx/8u-dev/rt
IDEs
NetBeans IDE 8.1 (requires JDK installation)
- make the installer files executable
$ chmod +x netbeans-8.1-javaee-linux.sh
- run the installer
$ ./netbeans-8.1-javaee-linux.sh
JDK for the NetBeans IDE:/usr/java/latest
- uninstall run the
uninstall.sh
file in the IDE installation directory $ cd ~/netbeans-8.1
$ ./uninstall.sh
- Add GlassFish Server as a Server in NetBeans IDE
Tools
->Server
->Add Server...
Install First Cup Tutorial in NetBeans
- open tab
Windows
->Services
(Ctrl+5) - expands
Servers
, right-clickGlassFish Server 4.1.1
, selectView Domain Update Center
and install if required - right-click
GlassFish Server 4.1.1
, selectStart
; after it started, selectView Domain Admin Console
- a new browser window opens and after GlassFish server has been initialized, click
Update Tool
inCommon Tasks
context menu - select
javaee-firstcup-tutorial
for installation inAvailable Add-Ons
, clickinstall
- this will create the project files at
~/glassfish-4.1.1/docs/firstcup
Library Path
$ cat GetSystemProperty.java
import java.util.Properties;
import java.util.Enumeration;
public class GetSystemProperty {
public static void main(String args[]) {
if( args.length == 0 ) {
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
System.out.println(key + " : " + value);
}
}
else {
for (String key: args) {
System.out.println(System.getProperty( key ));
}
}
}
}
$ javac GetSystemProperty.java
$ java GetSystemProperty java.library.path
$ /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Build from command line
with structure src/pkg/Temp.java
- compile
xps13@xps13 pkg]$ javac Temp.java
- run
[xps13@xps13 src]$ java pkg.Temp
Oracle Java
Learn
Java EE
- Java 6 EE
- Java 7 EE Tutorial
- Java 7 EE First Cup create web application consisting of Enterprise JavaBeans specification, a JAX-RS web service, and a JavaServer Faces component for the web front end
Tutorial
This tutorial is a guide to developing enterprise applications for the Java Platform, Enterprise Edition 6 (Java EE 6) using GlassFish Server Open Source Edition.
Windows
Test Java version
java -version
Edit environment variables
JAVA_HOME=C:\Program Files\Java\jre1.8.0_51
GlassFish
- glassfish.java.net World’s first Java EE 7 Application Server
- The Aquarium blog about GlassFish Server and open-source Java EE projects
Alternative application servers
- WebLogic
- WebSphere
- JBoss
- Tomcat (does not support .EAR files - compare .WAR)
JavaServer Pages (JSP)
- JavaServer Pages (JSP)
- JavaServer Pages Standard Tag Library (JSTL)
JavaServer Faces (JSF)
JavaServer Faces technology establishes the standard for building server-side user interfaces.
Unified Expression Language (EL)
Unified EL integrates the expression languages defined by JSP 2.0 and JSF 1.1. Thanks to the Unified EL, JSTL tags, such as the JSTL iteration tags, can now be used with JSF components in an intuitive way.
Build Tools
Apache Maven
- find maven version
mvn -version
Apache Ant
Compile Java source code
- install on Fedora 22
sudo dnf install ant
Example: Build SDMX library
- navigate to root directory
cd ~/Dropbox/GitHub/SDMX/JAVA
- clean the subtree
ant clean
- compile the code
ant compile
- build and install the java library (this will also copy the generated jar file in the dist subdir and in the correct library directories of each connector)
ant dist
Command line usage
- navigate to root directory
cd ~/Dropbox/GitHub/SDMX/JAVA
- run query
java -classpath dist/lib/SDMX.jar it.bancaditalia.oss.sdmx.util.GetTimeSeries ECB EXR.A.USD.EUR.SP00.A
- open the SDMX Helper tool
java -jar dist/lib/SDMX.jar
- run with different java version
$JAVA_HOME/bin/java -classpath dist/lib/SDMX.jar it.bancaditalia.oss.sdmx.util.GetTimeSeries ECB EXR.A.USD.EUR.SP00.A
Links
- Java Code Geeks javacodegeeks.com
- spring by Pivotal spring.io: Guides
WebJars
WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files.
- Explicitly and easily manage the client-side dependencies in JVM-based web applications
- Use JVM-based build tools (e.g. Maven, Gradle, sbt, …) to download your client-side dependencies
- Know which client-side dependencies you are using
- Transitive dependencies are automatically resolved and optionally loaded via RequireJS
- Deployed on Maven Central
OCA
- OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808
Self-assessment
What is the result of the following class? (Choose all that apply)
1: public class _C {
2: private static int $;
3: public static void main(String[] main) {
4: String a_b;
5: System.out.print($);
6: System.out.print(a_b);
7: } }
C. Compiler error on line 4.: The local variable a_b
may not have been initialized.
What is the result of the following code?
String s1 = "Java";
String s2 = "Java";
StringBuilder sb1 = new StringBuilder();
sb1.append("Ja").append("va");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(sb1.toString() == s1);
System.out.println(sb1.toString().equals(s1));
C. true is printed out exactly three times: sb1.toString() == s1
is false
.
What is the output of the following code? (Choose all that apply)
1: interface HasTail { int getTailLength(); }
2: abstract class Puma implements HasTail {
3: protected int getTailLength() {return 4;}
4: }
5: public class Cougar extends Puma {
6: public static void main(String[] args) {
7: Puma puma = new Puma();
8: System.out.println(puma.getTailLength());
9: }
10:
11: public int getTailLength(int length) {return 2;}
12: }
The code will not compile because of line 3: cannot reduce the visibility of the inherited method from HasTail
What is the output of the following program?
1: public class FeedingSchedule {
2: public static void main(String[] args) {
3: boolean keepGoing = true;
4: int count = 0;
5: int x = 3;
6: while(count++ < 3) {
7: int y = (1 + 2 * count) % 3;
8: switch(y) {
9: default:
10: case 0: x -= 1; break;
11: case 1: x += 5;
12: }
13: }
14: System.out.println(x);
15: } }
C. 6: x(3,2,1,6)
What is the output of the following code snippet?
System.out.print("a");
try {
System.out.print("b");
throw new IllegalArgumentException();
} catch (RuntimeException e) {
System.out.print("c");
} finally {
System.out.print("d");
}
System.out.print("e");
D. abcde
What is the result of the following program?
1: public class MathFunctions {
2: public static void addToInt(int x, int amountToAdd) {
3: x = x + amountToAdd;
4: }
5: public static void main(String[] args) {
6: int a = 15;
7: int b = 10;
8: MathFunctions.addToInt(a, b);
9: System.out.println(a); } }
B. 15: method addToInt
does not modify a
.
What is the result of the following code?
int[] array = {6,9,8};
List<Integer> list = new ArrayList<>();
list.add(array[0]);
list.add(array[2]);
list.set(1, array[1]);
list.remove(0);
System.out.println(list);
B. [9]: assuming that List
and ArrayList
are imported
What is the output of the following code?
1: public class Deer {
2: public Deer() { System.out.print("Deer"); }
3: public Deer(int age) { System.out.print("DeerAge"); }
4: private boolean hasHorns() { return false; }
5: public static void main(String[] args) {
6: Deer deer = new Reindeer(5);
7: System.out.println(","+deer.hasHorns());
8: }
9: }
10: class Reindeer extends Deer {
11: public Reindeer(int age) { System.out.print("Reindeer"); }
12: public boolean hasHorns() { return true; }
13: }
E. DeerAgeReindeer,false: private method hasHorns
with value false
cannot be overwritten with public method
Which of the following statements are true? (Choose all that apply)
A. Checked exceptions are intended to be thrown by the JVM (and not the programmer). B. Checked exceptions are required to be caught or declared. C. Errors are intended to be thrown by the JVM (and not the programmer). D. Errors are required to be caught or declared. E. Runtime exceptions are intended to be thrown by the JVM (and not the programmer). F. Runtime exceptions are required to be caught or declared.
A, D, F
Which are true of the following code? (Choose all that apply)
1: import java.util.*;
2: public class Grasshopper {
3: public Grasshopper(String n) {
4: name = n;
5: }
6: public static void main(String[] args) {
7: Grasshopper one = new Grasshopper("g1");
8: Grasshopper two = new Grasshopper("g2");
9: one = two;
10: two = null;
11: one = null;
12: }
13: private String name; }
D. Immediately after line 10, only one grasshopper object is eligible for garbage collection.