Programming in Java - advanced techniques
Wrocław University of Science and Technology
Computer Science, Faculty of Electronics, 6 semester
Radosław Lis, 241385
Applications made for university course Programming in Java - advanced techniques
.
cw1 - Sorting algorithms
cw2 - Desktop sorting application using sorting methods from cw1
cw3 - Console application for JVM research (different heap size, reference types) using ReflectionAPI
cw4 - Custom JavaBean component with all property types
cw5 - RMI application - clients who want to sort their data by available sorting servers
cw6 - Desktop application for managing bike trips. Using JDBC, MySQL and JAXB
cw7 - Simulation of ring network by using SOAP technology and TCP/IP Sockets
cw8 - Java Security app - policytool, Cipher, digital signature etc.
cw9 - Desktop application deployed by using JavaWS, JNLP and Apache Tomcat for playing in 5,5,4-game
cw10 - Minimax algorithm with alpha-beta prunings in 5,5,4-game using Javascript (Nashorn) and C++ (JNI)
cw11 - Little transformation of cw3
with Spring Beans and JMX
- IDE for Java (e.g. Eclipse, Intellij IDEA)
- Java
Application provides an interface for sort list of IElement objects (FloatElement or IntElement) consisting of key and value. Currently available algorithms:
- Quick Sort,
- Counting Sort,
- Pigeonhole Sort,
- Insert Sort.
You can simple add some algorithm by using the current convention (inheritance from AbstractSorter abstract class).
Create a list in Test.java and run by choose one of algorithm to sort it.
Desktop, internationalized JavaFX application using algorithms from cw1 for sorting lists. You can load data from a file (rules in Menu bar), add manually or generate using random methods. Depending on the region there are showed other decimal separators (e.g. ','
in Poland or '.'
in USA), other format of current dates and obviously other strings by using Resource Bundles
. You can choose:
- Poland (by default),
- USA,
- England,
- France,
- Germany,
- Japan.
Application shows current number of elements and by using Choice Format class supports the right declension of words in all language versions. There is also a great feature for saving sorted lists to .txt
files in a format readen by the app.
- Use cw2. jar file and in command line (xyz is your path to javafx-sdk):
java -jar --module-path xyz\javafx-sdk-11.0.2\lib --add-modules=javafx.controls,javafx.fxml cw2.jar
- Import all files into the project and configure build path
$ git clone https://github.com/radosz99/Programming-in-Java-Advanced-Techniques.git
$ cd Programming-in-Java-Advanced-Techniques
$ cd cw3
$ mvn install
$ cd target
$ java *A* *B* -Djava.awt.headless=true -jar cw3-1.0-SNAPSHOT-jar-with-dependencies.jar *C* *D* *E* *F* *H*
VM options (not required)
- A = minimum heap size for JVM in megabytes, e.g.
-Xms128m
, - B = maximum heap size for JVM in megabytes, e.g.
-Xmx256m
,
Arguments (required):
- C = number of seeds (lists to sort), e.g.
500
, - D = number of elements in seed, e.g.
8000
, - E = number of sorting threads, e.g.
16
, - F = type of reference to store a key (seed) in the map - cache -
SOFT
,HARD
orWEAK
, - H = type of reference to store a value (dataset) in the map - cache -
SOFT
,HARD
orWEAK
.
Console, multi-threaded application for testing JVM (Java Virtual Machine) capabilities. Synchronized threads try to sort (with algorithms from cw1) selected (random generated) datasets identified by a seed. Appropriate classes with sorting methods are dynamically loaded during the program execution from cw1.jar
by using ClassLoader and reflection mechanism. Sorted datasets are putting in cache (ReferenceMap) by using chosen references - WEAK
, SOFT
or HARD
, depending on which Garbage Collector may delete dataset from memory. By setting another parameters (mainly B - maximum heap size for JVM) many behaviours can be observed - such as segmentation faults (OutOfMemoryError
) if references are HARD
or nearly 100% misses if the maximum size is low and references are WEAK
, and finally, smooth, continuous running by using SOFT
references. Application generates every 3 seconds raport which gives information about cache misses and overall sorted datasets compared to stored datasets in the cache at this moment.
By the following call you set maximum JVM memory to 256 megabytes and run 16 threads that are trying to sort 500 seeds with 8000 elements each and put it to the cache. Seed reference is WEAK
and dataset references are SOFT
. Below - in the console output - it can be observed that declared references provides constant number of datasets stored in the cache, despite the continuous sorts performed by the threads:
$ java -Xms128m -Xmx256m -Djava.awt.headless=true -jar cw3-1.0-SNAPSHOT-jar-with-dependencies.jar 500 8000 16 WEAK HARD
JavaBean with three types of properties:
- Simple Property - BeanString
- Bound Property - BeanValue
- Constrained Property - BeanFont
Add MyBean.jar
from cw4/Bean
to cw4/BeanUsageExample
build path and have fun with such a great technology as Java Beans are!
You can change bean properties by modifying Bean
, BeanInfo
and Controller
in cw4/bean/...
and then:
$ javac Bean.java BeanInfo.java Controller.java
$ jar cf MyBean.jar Bean.class BeanInfo.class Controller.class
Originally made in Eclipse IDE. Each update of the bean properties implies the need to refresh the project.
Exemplary screenshot with stack trace of an error caused of an attempt to change beanFont property to a blacklisted font (fell on poor Cambria). That property is constrained and when it is about to change, the listeners are consulted about the change and they say that Cambria is not suitable:
Application simulates process of running distributed applications by using RMI.
Client applications (ClientApplication.java
) allow you to generate random data and sort them using a server (ServerApplication.java
) selected from the server list obtained thanks to the Central (CentralApplication.java
).
To test distributed applications:
- Configure VM arguments as it is described here in 3rd point
- Run exactly once
cw5/src/rmi/CentralApplication.java
- it is the Central (list of servers) - Run
cw5/src/rmi/ClientApplication.java
as many as you want - it is the Client - Choose algorithm and run
cw5/src/rmi/ServerApplication.java
as many as you want - it is the Server - Generate data, sort it by using one of registered servers, whatever you want to do
The order in which clients and servers are started doesn't matter. If you want to run more than one instance of Client or Server you must choose Allow parallel run
in Edit Configurations
in IntelliJ.
Basic JDBC desktop application in JavaFX for managing bike trips. By using JAXB it allows serializing and unserializing object to write it in XML files.
Create MySQL database and run script from cw6/skrypt.txt
for some data to test the application.
Application simulates working of ring network topology. There is a finite number of working applications that can send and receive messages from others running aplications.
Ring topology works at follows:
A -> B -> C -> D -> A
Messages are sending by SOAP and can be send as Unicast or Broadcast.Choose Allow parallel run
in Edit Configurations
in IntelliJ and run as many apps as you want. To close the ring you must click Zamknij pierścień
button in ultimately last application.
Fun (XD) with Java Security.
$ openssl genrsa -out keypair.pem 2048
$ openssl rsa -in keypair.pem -outform DER -pubout -out public.der
$ openssl pkcs8 -topk8 -nocrypt -in keypair.pem -outform DER -out private.der
$ keytool -genkey -alias signJar -keystore my-store
$ jarsigner -keystore my-store -signedjar cw1signed.jar cw1.jar signJar
By using policytool
add grants and save it in mypolicy
file.
grant {
permission java.lang.RuntimePermission "createClassLoader";
};
grant {
permission java.io.FilePermission "<<ALL FILES>>", "write";
};
grant {
permission java.io.FilePermission "<<ALL FILES>>", "read";
};
- Exporting public key -
keytool -export -keystore my-store -alias signJar -file PublicKey.cer
- Importing public key -
keytool -import -alias foreign -file PublicKey.cer -keystore receiver-store
- Displaying certificate -
keytool -printcert -file PublicKey.cer
- Displaying key from store -
keytool -list -keystore my-store -alias signJar
- Verifying jar -
jarsigner -verify cw1signed.jar
In project main folder (create keys in TARGET folder) before running it):
$ mvn install
$ cd target
$ java -Djava.security.manager -Djava.security.policy=mypolicy -jar cw8-1.0-SNAPSHOT.jar
Application as a foundation for cw10.
Currently in cw9
folder we have cw10
. To see cw9
you must go back to commit b186556
.
5,5,4-game in JavaFX. You can play with computer (random algorithm) or with other person on the same app (left and right clicks).
In project main folder:
$ mvn install
$ cd target
$ keytool -genkey -alias signJar -keystore my-store (once)
$ jarsigner -keystore my-store -signedjar game.jar game-1.0-SNAPSHOT.jar signJar
Then move game.jar
and Game.jnlp
to C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\ROOT
and:
$ cd C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin
$ startup
$ javaws http://localhost:8080/Game.jnlp
Currently all files are in cw9/ folder.
5,5,4-game in JavaFX using Javascript Script Engine and JNI. You (X) play with AI (O).
Build JNI library in resources
folder by typing:
$ ./build_library.sh
Then open project in IntelliJ, import JavaFX libs and in Edit configurations
in VM options
type (xyz
are appriopriate system paths):
$ -Djava.library.path=...xyz/resources/ --module-path "...xyz/javafx-sdk-11.0.2/lib" --add-modules=javafx.controls,javafx.fxml
Then simply run Main.java
.
Idea of the application is the same as in cw3, algorithms are loaded using Reflection API, but the app is configured as MBean by using Spring annotations and attributes of this bean (cache size and number of sorting threads) can be dynamically changed during the runtime in e.g. jconsole
tool.
Simply open project in IDE (e.g. IntelliJ) and run main function in main/java/pl/advanced/Main.java
. Then open terminal, type jconsole
and connect with the running process:
Then go to the MBeans
section, expand cw11
and test Bean methods by setting size (1=<
) of cache (amount of stored keys with datasets) and number (0=<
) of sorting threads.
You can observe flow of the application in the console logs:
Thread no 0, seed no 167, QuickSort algorithm, execution time is 1 ms
Thread no 4, seed no 311, QuickSort algorithm, execution time is 2 ms
Thread no 3, seed no 364, InsertSort algorithm, execution time is 139 ms
Thread no 5, seed no 110, CountingSort algorithm, execution time is 77 ms
Thread no 9, seed no 238, QuickSort algorithm, execution time is 1 ms
Thread no 8, seed no 487, CountingSort algorithm, execution time is 69 ms
Thread no 6, seed no 93, CountingSort algorithm, execution time is 71 ms
Thread no 3, seed no 102, PigeonHoleSort algorithm, execution time is 379 us
- Go Window -> Preferences -> Java -> Build Path -> User Libraries -> New and write JAVAFX_11. Then left click on JAVAFX_11, click Add External JARs...
and add all JARs from
...\javafx-sdk-11.0.2\lib
. - Right click on the project and go Build Path -> Configure Build Path -> Add Library -> User Library -> Next and choose JAVAFX_11.
- Go Run -> Run Configurations -> Arguments and in VM arguments field write:
--module-path "xyz\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml
(xyz to your path to javafx-sdk). Then Apply -> Close.
- Go Help -> Install New Software, click Add and in
Location
field paste
https://download.eclipse.org/efxclipse/runtime-nightly/site/
- Go Help -> Install New Software, click Add and in
Location
field paste
https://download.eclipse.org/efxclipse/updates-nightly/site/
- Restart IDE
- Go to Gluon site and in section Download Scene Builder for Java 11 download version for your OS.
- Go Window -> Preferences -> JavaFX and in
SceneBuilder executable
field click browse and find your .exe file (...\SceneBuilder\SceneBuilder.exe
) and in *JavaFX 11+ SDK
field click browse and find your sdk folder (...\javafx-sdk-11.0.2
)
- Go Window -> Preferences -> General -> Workspace -> and in
Text file encoding
field choose Other -> UTF-8.
If any of this:
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
In the enviroment variable JAVA_HOME set the folder on Java 11 JDK (C:\Program Files\Java\jdk-11.0.6
).
If any of this:
java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x552f46e4) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module
Error: JavaFX runtime components are missing, and are required to run this application
Configure VM argument as it is described here in 3rd point.