JAVA and JAR’s from the command line

This is a small little tutorial which will explain how to create your own little Java application which have dependencies which are in other JAR files

First create the following directory structure:

java_test
    |
    |__myapp
    |     |__src
    |     |   |__ch
    |     |       |__eitchnet
    |     |            |__java
    |     |                 |__Main.java
    |     |__META-INF
    |     |    |__MANIFEST.MF
    |     |__bin
    |
    |__mydep
    |     |__src
    |     |   |__ch
    |     |       |__eitchnet
    |     |            |__helloworld
    |     |                 |__HelloWorld.java
    |     |__bin
    |
    |__deploy
        |__lib

The MANIFEST.MF file has the following content:

	Main-Class: ch.eitchnet.java.Main
	Class-Path: mydep.jar

The Main.java file has the following content:

  package ch.eitchnet.java;

  import ch.eitchnet.helloworld.HelloWorld;

  public class Main {
      public static void main(String[] args) {
      HelloWorld hw = new HelloWorld();
      System.out.println("HelloWorld instances: " + hw.getInstancesCount());
    }
  }

The HelloWorld.java has the following content:

  package ch.eitchnet.helloworld;

  public class HelloWorld {
    private static int instances;

    public HelloWorld() {
      System.out.println("Created HelloWorld object");
      instances++;
    }

    public int getInstancesCount() {
      return instances;
    }

    protected void finalize() {
      instances--;
    }
  }

Change your directory to be in the root of our project

Compile the dependency first:

find mydep/src/ -name "*.java" | xargs javac -d mydep/bin/

Should you be using a windows system, then you have compile file by naming each one. See the java help file for more information

Create the jar:

jar cvf ./deploy/mydep.jar -C ./mydep/bin ./

Compile the app:

find myapp/src/ -name "*.java" | xargs javac -classpath ./deploy/*.jar -d myapp/bin/

Should you be using a windows system, then you have compile file by naming each one. See the java help file for more information

Create the jar:

jar cmvf ./myapp/META-INF/MANIFEST.MF ./deploy/myapp.jar -C ./myapp/bin ./

Run the app by specifying everything explicitly:

java -classpath ./deploy/myapp.jar:./deploy/mydep.jar ch.eitchnet.java.Main

Or by running directly allowing the MANIFEST.MF to do its magic:

java -jar ./deploy/myapp.jar

tutorial files

grretings eitch

JSF and PDF’s

This is a quick snipped on transmitting a PDF in a JSF context:

byte[] bytes = getPDFStreamAsArray();
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) faces.getExternalContext()
.getResponse();
response.reset();
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
response.setHeader("Content-disposition", "inline;filename="file.pdf""); // inline or attachment
response.setHeader("Cache-Control", "cache, must-revalidate");
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
faces.responseComplete();

JSF and EL (Expression Language) rant !

Beware people… Referring to missing methods in a Bean from a XHTML page using the expression language yields an exception, but referring to a *private* method, doesn’t to nothing. No exception, no message, just plain and simply nothing.

I spent hours trying to understand why my application didn’t work, and now I know it: Be careful that your exposed method is not marked as *private* in your bean!!

eitch

Java RMI quick and dirty

Your object which will be accessible through RMI must meet the following requirements:

  • have its own interfaces which extends java.rmi.Remote
  • all methods must “throws RemoteException
  • all returned returned objects must implement java.io.Serializable

Starting a RMI server:

  • Set system property: System.setProperty("java.rmi.server.hostname", "hostname");
  • create registry: Registry registry = LocateRegistry.createRegistry(1099);
  • export Object: RemotableObject stub = (RemotableObject) UnicastRemoteObject.exportObject(remoteObject, 0);
  • bind object: registry.rebind("RemoteName", stub);

Remote call:

  • get Registry: Registry registry = LocateRegistry.getRegistry("hostname");
  • get Remote object: RemotableObject remoteObject = (RemotableObject) registry.lookup("RemoteName");
  • call method: remoteObject.remoteMethod();

Apache Tomcat 6.0 configuration for developers

Just a quick blog on configuring some specific Tomcat 6.0 settings:

Memory Heap Size on a unix based system

Open the file %tomcat_install_folder%/bin/startup.sh

Add the line:

export CATALINA_OPTS="-Xms256m -Xmx512m"

before the line:

exec "$PRGDIR"/"$EXECUTABLE" start "$@"

to have a memory heap of at least 256mb and max of 512mb.

Memory Heap Size on a Windows based system

On a windows platform you have to set a environment variable called “CATALINA_OPTS”, with the value:

-ms128m -mx256m

You can this by right clicking the icon of your “My Computer” and then choosing properties. Under “System” I think you will then find the environment variable editor. Just add it there.

Activating servlet reloading

Open the file %tomcat_install_folder%/conf/context.xml

Change the element tag “Context” from so:

<Context>

to so:

<Context reloadable="true" privileged="true">

have fun coding webapps
eitch

Remote Desktop from Ubuntu

I don’t want to search for the right commands everytime I want to connect to a windows machine with RDP when I’m on a Linux machine running Ubuntu or Kubuntu that is. The best tool I know, that really does the work is rdesktop.

Install this package:

sudo apt-get install rdesktop

And then just connect with this line:

rdesktop -u user -p password -k de-ch -g 1280x1024 server

This also shows how to use a Swiss German keyboard layout, as I have searched for it many times. The thing is, that it is not an underscore, as many may think.

Simple Configuration for Log4j

Ever wanted to use Log4j, but didn’t know how to configure it? Or you don’t remember how to configure it in a jiffy?

Just add the following two lines in your application, like say in the main()-method and then your’re done:


BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d %5p [%t] %C{1} %M - %m%n")));
Logger.getRootLogger().setLevel(Level.INFO);

The first line sets logging to go to the console and it will look something like this:

2008-04-18 11:21:33,395 INFO [main] TestApp main - exists: true

If you want a different logging level, then just take Level.DEBUG, or whatever else suits you.

MySQL new user SQL statements

A quick and dirty explanation to create a new MySQL datbase with a user

Create the user:

  • CREATE USER 'hibtest'@'localhost' IDENTIFIED BY '*******';
  • Set some permissions:

  • GRANT USAGE ON * . * TO 'hibtest'@'localhost' IDENTIFIED BY '*******' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0MAX_USER_CONNECTIONS 0 ;
  • Create the database:

  • CREATE DATABASE IF NOT EXISTS `hibtest` ;
  • Grant the privileges on the database:

  • GRANT ALL PRIVILEGES ON `hibtest` . * TO 'hibtest'@'localhost';
  • Apache and redirecting a url

    This is a quick blog for me to remember how to do a proxy in apache:


    <VirtualHost *:80>
    ServerName www.someurl.com
    ServerAlias www.someurl.com
    ServerAdmin someone@someurl.com

    ProxyPreserveHost On

    ProxyRequests Off

    <Proxy *>
    Order deny,allow
    Allow from all
    </Proxy>

    ProxyPass / http://999.888.777.666:8080/
    ProxyPassReverse / http://999.888.777.666:8080/
    </VirtualHost>