Τετάρτη, 17 Ιουνίου 2015

To find out which Java version a class was compiled with you can use the javap tool to extract the major/minor versions of the class.

javap -v com/test/MyClass.class  | grep -E 'major|minor'

Here is the mapping of Java version to class major version:

J2SE 8 = 52 (0x34 hex),
J2SE 7 = 51 (0x33 hex),
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).
Alternatively, the unix "file" command also tells you this information:
file com/test/MyClass.class

Πέμπτη, 13 Φεβρουαρίου 2014

Simple TCP listener for logging HTTP requests

In case you are developing an http client and you want to examine the exact contents of an http request there is a fast way to do so, by using the always handy netcat tool. nc comes preinstalled on Mac. So to setup a listener on e.g. port 8080 you may use the following command:

sudo nc -l 8080

But the client will need a reply too so, better use:

echo -e 'HTTP/1.1 200 OK\r\n' | sudo nc -l 8080;

Now direct your http request to localhost:8080. nc will print the entire http request on standard output and will reply with a status 200 OK.

Now, if you want the listener to keep listening for the next request, you may use the following bash script. The script also allows terminating the program using Control-c. Otherwise, the while loop would go on forever.

#!/bin/bash

[ "$#" -eq 1 ] || { echo "Usage $0 "; exit 1; }

PORT=$1

killed=0
trap '{ echo "sigint"; killed=1; }' SIGINT

echo "Listening for connections on port $PORT"

while (( !killed )); do
echo '======================='
echo -e 'HTTP/1.1 200 OK\r\n' | sudo nc -l $PORT;
echo ''
done

Πέμπτη, 16 Μαΐου 2013

How to find the serial version UID of a java class

If tools such as serialver and javap are hard to use, you can simply use the following code:


public static void main(String[] args) throws IOException, ClassNotFoundException {
     ObjectStreamClass descriptor = 
           ObjectStreamClass.lookup(YourClassHere.class);
     System.out.println("serialVersionUID:" + descriptor.getSerialVersionUID());
}

Πέμπτη, 2 Μαΐου 2013

Search and replace with regular expressions in Eclipse

Have you ever wanted to do some refactoring of code which involves replacing the same kind of code in many places? Let say for example you need to add an attribute to all xml elements of certain type but the value of the attribute depends on an already existing value. Ok... you can either prepare yourself for hours of boring typing or just use the magic of regular expressions inside Eclipse.

Let's say you want to convert this xml:


 id="12435" title="Eclipse rules" />
 id="54645" title="Regular expressions cookbook" />
 id="46576" title="Java is great" />

to this:

 id="12435" title="Eclipse rules" isbn="0000-12435-0000" />
 id="54645" title="Regular expressions are magic" isbn="0000-54645-0000" />
 id="46576" title="Java is great" isbn="0000-46576-0000" />

And, yeah, assume you have a few hundreds of these lines :)

To save your hands and your time, you can just do the following:

Search for:  
id="(\d+)" title="(.*)"
(with regular expression option checked)

And replace with:
id="$1" title="$2" isbn="0000-$1-0000"

This works both within a file and across many files. Voila, you're done!

Τετάρτη, 13 Μαρτίου 2013

Check open ports on Mac OSX

Netstat works differently in Mac and Unix. So in Mac it's better to use lsof (list open files).

lsof -i -P | grep "LISTEN"

How to clear a project from its eclipse metadata

Say you have a Maven project that you have imported into Eclipse and you would like to clean re-import it. Or maybe you have created a new project and you want to import it into version control without its Eclipse metadata. Simple! Delete the project from eclipse, then open Terminal, go to the root directory of the project and run:

find . \( -name .project -o -name .settings -o -name .classpath \) -exec rm -rf '{}' \;

The above command deletes files and directories without warning. Use it with caution!

You may get "No such file or directory" messages. That means the script already deleted that file or directory so no problem.

In addition, if you want to delete IntelliJ metadata you may do the following:

find . \( -name .idea -o -name *.iml \) -exec rm -rf '{}' \;

Specify or check active maven profiles at command line

To force a specific Maven profile to active and force another to non active (which would otherwise be active) you can use the -P parameter. In bash shell you will need to escape the ! character with a \

So for example to run the validate phase with prof-one active and prof-two non active run the following:

mvn validate help:active-profiles -P prof-one,\!prof-two

The help:active-profiles goal prints debug info about the active profiles.