Dieses Blog durchsuchen

Dienstag, 20. April 2010

Eclipse Galileo 3.5.1 Index Problems

After a while working with eclipse Galileo SR1 the startup of the IDE slows down. Beside this I frequently got an error like that:

Class file name must end with .class

To fix this I've done the following:
  1. Close Eclipse
  2. Delete /.metadata/.plugins/org.eclipse.jdt.core/*.index
  3. Delete /.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
  4. Start Eclipse again
The eclipse bug list mentioned the behaviour in defect 286379 fixed for Milestone 3.6 M3.

Mittwoch, 17. März 2010

Instantiate a Generic Type in Java

Once you used generics for a while you might stumble upon the following question:

public class MySample {
       private     T    mySample = null;


       public MySample() {
               mySample = new T();  // well this would be fine, but it doesn't work because the compiler doesn't know the type at this time!
      }
}

Ok, fair enough the compiler doesn't know if T is an interface or an object and couldn't instantiate the type. If your T is a class you could solve the question with this:



public class MySample {
       private     T    mySample = null;

       public MySample(Class _class) {
               mySample = _class.newInstance();
      }
}


This should work with the little overhead of specifying the class in the generic and the constructor:



  MySample  sample = new MySample(Whatever.class); 

  

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?
Answer:

 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):
#!/bin/bash
 

# Information about the NAS access
    username=MY_NAS_USER
    password=MY_NAS_PASS
    hostname=MY_NAS_HOST

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

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

# Create a mount as the current logged in user
do_mount() {
    if /sbin/mount|grep -q $1; then
    # echo "Mounted"
        /sbin/umount $1
    fi
    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.
HINT:
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.

Min .bashrc


# *****************************************************************************
# bashrc
# *****************************************************************************
SHELL=/bin/bash
PATH=$PATH:$HOME/bin:/usr/local/bin
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'