Tuesday, March 3, 2015

Java: running with Maven



Compiling with Maven

note:

  • mvn clean install
  • location of created jar



uki@ WebSocketClientServer $ mvn clean install

[INFO] Scanning for projects...

[INFO]                                                                         

[INFO] ------------------------------------------------------------------------

[INFO] Building WebSocket-GlassFish 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

...

.to /Users/uki/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------







Using variables to store long values



uki@ ~ $ export WEBSOCKET_JAR=~/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jaruki@ ~ $ echo $WEBSOCKET_JAR/Users/uki/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jar




Starting SERVER



$ java -cp $WEBSOCKET_JAR edu.clcillinois.websocket.server.WebSocketServer -h localhost -p 8025




Setting homeName to: localhostStarting the server.Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.ServerContainerFactory createINFO: Provider class loaded: org.glassfish.tyrus.container.grizzly.GrizzlyEngineMar 03, 2015 3:56:54 PM org.glassfish.grizzly.http.server.NetworkListener startINFO: Started listener bound to [0.0.0.0:8025]Mar 03, 2015 3:56:54 PM org.glassfish.grizzly.http.server.HttpServer startINFO: [HttpServer] Started.Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.Server startINFO: WebSocket Registered apps: URLs all start with ws://localhost:8025Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.Server startINFO: WebSocket server started.
Type 'q' and Enter to stop server: 



Starting Client(s)




uki@ CNH_PROD $ java -cp $WEBSOCKET_JAR edu.clcillinois.websocket.client.WebSocketClient -h localhost -p 8025
[DEBUG] 2015-03-03 16:02:44,354 edu.clcillinois.websocket.client.WebSocketClient main - Starting WebSockets clientSetting homeName to: localhost



Monitoring Java processes and Killing them


$ ps | grep java
30227 ttys001    0:00.00 grep java

30203 ttys002    0:01.37 /usr/bin/java -cp ~/.m2/repository/edu/clcillinois/cit137/xyz.jar edu.clcillinois.websocket.server.WebSocketServer -h localhost -p 8025

$ kill -9 30203 uki@ SimpleWebSocketServer $ ps | grep java30230 ttys001    0:00.00 grep java


Maven pom.xml example

note: 
  • group id
  • artifact id
  • version
  • start-class















Maven Dependencies


note:
  • group id
  • artifact id
  • version








Maven build


note: 
  • plugins 
  • Java version 1.6, 1.7 or 1.8
  • TODO change to Java 1.6 and observer what fails
  • jar with dependencies







Friday, February 27, 2015

Android: UI: horizontal divider

In this recipe we will add a thin gray line divider to our UI



       <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="6dp"
            android:alpha="0.5"
            android:background="@color/bar_grey_light"/>



Android: xmlns res-auto

You can use "res-auto" URI instead of defining custom packages:

so...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res-auto"

instead of...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res/com.my_company.xyz"



and then use your tag...

       <com.my_company.xyz.control.MyPanelView
                android:id="@+id/jdhfkjsd"
                style="@style/kjdlksjdhjhk"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                my_tag:header="@string/kjsdlfkdslkfj"

Java: Random


In this example we create a random speed jump by a certain constant.

private Random random = new Random();

private static final double SPEED_M_PER_S = 2.2352;  /* 5 MPH */
private static final double MAX_RANDOM_DELTA = 0.13;

double speed = SPEED_M_PER_S 
+ Math.min(MAX_RANDOM_DELTA, random.nextDouble()) 
* (random.nextBoolean() ? -1 : 1);

Thursday, February 26, 2015

Java: Unsupported major.minor version 52.0

This error basically means that you compiled your Java code with the version higher than that on the target machine you are trying to run it on.

 $ java -versionjava version "1.8.0_20-ea"


Java 1.8 == 8 == 52 (go figure why this confusion)

J2SE 8 = 52,
J2SE 7 = 51, 
J2SE 6.0 = 50, 
J2SE 5.0 = 49, 
JDK 1.4 = 48, 
JDK 1.3 = 47, 
JDK 1.2 = 46, 
JDK 1.1 = 45

Solution 1: update Java version on target machine (often impossible)
Solution 2: compile with lower Java version (skip on fancy language features e.g. lambdas) 

e.g. using Maven

   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
               <!-- target has java version "1.7.0_60", to be safe we use 1.6  -->
               <source>1.6</source>
               <target>1.6</target>
            </configuration>
         </plugin>


e.g. using Java Compiler

javac -target 1.6 HelloWorld.java

Tuesday, February 24, 2015

Java Locale p4

Create FXML layout



Java: Locale p3

in this tutorial part we will EXTEND ResourceBundle.Control to make sure Java properly reads our language properties files.


Step:
create public class UTF8Control extends ResourceBundle.Control

Step:
find package java.util public abstract class ResourceBundle


Step:
find public static class Control

Step:
find public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)


Step:

copy this method to your new class

Step:


CHANGE only what you see in line 76





Java: Locale p2

In this part we will create a simple utility class that will choose the right Locale

Note:

  • using static method
  • we are passing in character country code
  • we are returning Locale
  • this could grow to 180+ cases





Java: Locale p1

In this tutorial we will create JavaFX app that uses selected Locale to pull i18n (Internationalization) text.


Note:

  • round buttons
  • selected county name
  • text in different character set
  • money notation depending on selected country





Step : create a new IntelliJ Module

  • Module name DisplayLocale
  • create packages as shown in the picture below
  • find country flags icons about 64x64x
  • get some translation texts

Step: Create your translation .profile files

Example Australian English file

morning_greeting=G'day Mate!evening_greeting=G'evening Mate!


Step: Create class DisplayLocaleMain extends Application

Note:
  • import java.util.*
  • import javafx.*
  • extends Application -- JavaFX Application
  • implements Initializable -- automatic injection of Locale 
  • main method
  • @FXML - annotation
  • @override start method
  • css/buttons.css





Note: 

  • @Overrite initialize method
  • Currency.getInstance(bundle.getLocale());
  • NumberFormat.getCurrencyInstance(bundle.getLocale());

Note:
  • FXMLLoader fxmlLoader
  • ResourceBundle.getBundle("bundles.Language", locale, new UTF8Control());
  • fxmlLoader.setResources(languageBundle);
  • getResource("views/TranslatedPhrasesPane.fxml");
  • Pane pane = (BorderPane) fxmlLoader.load(url.openStream()); stage.getScene().getRoot()).getChildren().get(1); LanguageButtonEvent implements EventHandler
  • @Override handle method
  • LocaleBuilder.byLanguageCode(button.getId());







see next part