Dieses Blog durchsuchen

Dienstag, 16. Februar 2010

Java File Format Version

Once you're moving around for a while in Java based environments you might have seen an error like this

java.lang.UnsupportedClassVersionError: Bad version number in .class file

If you hit this kind of error than its time to realize that Java byte code might differ between JDK versions :-) You'll find a long list of explanations about the Java Class file format all over in the web (http://en.wikipedia.org/wiki/Class_%28file_format%29).
Here is a list of class file versions and the corresponding JDK

J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).

Ok thanks for the list but how do I get the version out of an existing class file?

 javap -v MyClass | more

this will print out something like this:

 class MyClass extends java.lang.Object
  minor version: 0
  major version: 50

Donnerstag, 4. Februar 2010

Mount network share at startup in MacOSx

I run a Buffalo TeraStation NAS server within my environment. To use it within a unix based machine like my IMac I just need to open a Finder and select the share on the server. That'll do the trick for instant access or manual connect. If you want to have your shares mapped before you logon do the following:

1. Create Script, which mounts your shares (see my sample below)

2. Add the script to the LoginHook
     sudo defaults write com.apple.loginwindow LoginHook /path/to/script
    (I store my script on MacOSX in /Library/Scripts)
. More infos on the LoginHook

Note: If you mount a share keep in mind that mounting the share with root privileges will only allow root to use the share. Because of that I used su $1 -c to run the mount as the current logged in user. 

Sample mount script for a NAS server with afp protocol for Mac OSX (if your NAS has no afp just change the mount parameter accordingly):

# Information about the NAS access

# The $1 is within login scripts the current user, which enters MacOsx   

# Create a mount dir if necessary
save_mkdir() {
    if [ ! -d $1 ];
        su $loginuser -c "mkdir $1"

# Create a mount as the current logged in user
do_mount() {
    if /sbin/mount|grep -q $1; then
    # echo "Mounted"
        /sbin/umount $1
    save_mkdir $1
    su $loginuser -c "/sbin/mount -t afp afp://$username:$password@$hostname/$2 $1"

echo "******************************************************"
echo "*** Unmount all network drives from boshuda"
echo "******************************************************"
/sbin/umount /Volumes/pictures
/sbin/umount /Volumes/music

echo "******************************************************"
echo "*** Mount all network drives from boshuda"
echo "******************************************************"
do_mount /Volumes/pictures pictures
do_mount /Volumes/music my_music

Dienstag, 2. Februar 2010

Include the output of a servlet within your jsp

Sometimes its useful to call a servlet and include the output of the servlet within your jsp page. The easiest way to call a servlet from a jsp is the jsp:include tag. Lets assume you have a jsp fragment like this


VERSION: <jsp:include page="/InfoServlet?name=VERSION"></jsp:include>


this will cause a call to the InfoServlet with the parameter name and the value VERSION. The output will be written by the InfoServlet.
The calling servlet has to use the same access method to the response as the jsp and should not close the stream. Otherwise the output of your jsp will stop immediately.
So if you don't know just use
response.getOutputStream() to write to the response.

Montag, 1. Februar 2010

Min .bashrc

# *****************************************************************************
# bashrc
# *****************************************************************************
PS1="\u \w$ "

export EDITOR=vi
export HISTCONTROL=ignoredups
# Cli Colors
export CLICOLOR=1
# use yellow for dirs
export LSCOLORS=dxfxcxdxbxegedabagacad
export LSCOLORS=exfxcxdxbxegedabagacad

# *****************************************************************************
# alias
# *****************************************************************************
alias ..='cd ..'
alias h=history
alias ip='ipconfig getifaddr en0'
alias l='ls -al'
alias lp='ls -p'
alias lt='ls -lt'
alias sys_prefs='open -a System\ Preferences'

Log4jdbc Configuration for WebLogic

I like to use log4jdbc (http://code.google.com/p/log4jdbc/) for logging DB statements within Java JDBC environment. Here is a short instruction how to set it up within a BEA/Oracle weblogic container.

1. Download log4jdbc and slf4j

2. Edit the startup script of you domain and add the following lines

set SAVE_JAVA_OPTIONS=%JAVA_OPTIONS% -Dlocal.url=t3://localhost:7001 -Dlog.dir=D:\logs %LOG4JDBC_OPTIONS%
set LOG4JDBC_OPTIONS=-Dlog4jdbc.drivers=oracle.jdbc.OracleDriver -Dlog4jdbc.sqltiming.warn.threshold=200 -Dlog4jdbc.sqltiming.error.threshold=500

3. This will register a log4jdbc driver for Oracle and the log level WARN will be used for statements longer than 200ms, as well as the ERROR level will be uses for statements which take longer than 500ms

4. You also have to add the necessary jar files to the startup classpath of your weblogic container

set SAVE_CLASSPATH=%CLASSPATH%;%LOG4JDBC_HOME%\log4jdbc3-1.1.jar;%LOG4JDBC_HOME%\slf4j-api-1.5.0.jar;%LOG4JDBC_HOME%\slf4j-log4j12-1.5.0.jar;%LOG4JDBC_HOME%\log4j-1.2.14.jar;%LOG4JDBC_HOME%\log4j.xml;%LOG4JDBC_HOME%\kdm_conf

as you see we need a log4jdbc, slf4j-api, slf4j-log4j and a log4j jar. The version should fit together and the log4j version has to be at least 1.2.14. The rest of the classpath links to the log4j config. A sample config for log4jdbc is here http://code.google.com/p/log4jdbc/source/browse/trunk/doc/log4j.xml.

5. Last step is the setup of your jdbc configuration. To activate the log4jdbc driver you could just change your current jdbc config file or the data source within the weblogic container. Here is a sample:
Original JDBC config

log4jdbc config


Thats it.

Keystore für Java Web Start Code Signing

Hier mal eine kleine Anleitung für die Erstellung eines Keystores, der zum Code Signing benutzt werden kann.

1. Neuen Keystore anlegen

keytool -genkey -keyalg RSA -keystore keystore.ks -alias myalias

Hier wird man nun nach allem möglichen Informationen gefragt, die später Inhalt des Zertifikates sind. Diese Informationen bekommen die Endbenutzer zu sehen, wenn sie z.B. auf eine Java Web Start Anwendung zugreifen, die mit diesem keystore signiert wurde.

2. Certrequest generieren

keytool -certreq -keystore keystore.ks -file csr.txt -alias myalias

Dieser Request muss nun von einer vertrauenswürdigen RootCa unterschrieben werden.

3. Generiertes Zertifikat für den Request importieren

keytool -import -file my.cert -alias myalias -trustcacerts -keystore keystore.ks

Es kann passieren das hier eventuell eine Fehlermeldung auftritt diese Fehlermeldung leigt meist am Inhalt des emfpangenen Zeritifkats. Hier darf nur der Text mit Begin und End drin stehen. Alle anderen Sachen haben hier nichts zu suchen.
Wenn es trotzdem nicht klappt, kann das Certificat auch erst im IE unter Datenschutz ->Zertifiakte importiert werden und danache als p7b (PKCS) exportiert werden.

4. RootCa Cert importieren

keytool -import -file rootca.cer -alias myrootca -trustcacerts -keystore keystore.ks

Das RootCa Zertifikat ist notwendig, falls die RootCa nicht im allgemeinen Java trusted RootCa File enthalten ist (cacerts). Mit dem oben aufgeführten Befehl wird das RootCa Zertifikat mit in den neu erstellten Keystore aufgenommen. Das RootCa Zertifikat kann einfach aus einer vorhandenen SSL Seite exportiert werden.