Tuesday, 21 January 2014

Run a java project written in eclipse or netbean from the command line

You know when the whole aspect of having so many IDEs to help you do things like compile and running of your code, yeah i was biten in the a** of late and was expect to provide a jar file needed to be executed by someone else but i needed to test the jar which has other jar dependence through a commandline. I knew I had to add the jar dependencies as parameters to the java binary, but I was unsure how to do that. After a bit of googling around , I found out how to achieve this. Here’s a skeleton :


java -classpath lib1.jar;lib2.jar package.otherpackage.classname argv1 argv2 ... argvn

If you have your .class files in a folder, let’s say the name of the folder is classes, then you need to add that folder to the classpath, so the skeleton becomes :


java -classpath classes;lib1.jar ...

I hope you get the picture. If you don’t know what jars to include, but your project works in eclipse, you can open the .classpath file and extract the entries with kind=”lib”. Here is a sample .classpath file from a project :


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
 <classpathentry kind="src" path="src"/>
 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
 <classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
 <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
 <classpathentry kind="lib" path="lib/commons-io-1.4.jar"/>
 <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>
 <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
 <classpathentry kind="lib" path="lib/xml-apis-1.3.04.jar"/>
 <classpathentry kind="output" path="bin"/>
</classpath>

You can easily parse this file and extract them. This was how i got through the task.


Hope this helps someone.

Removing system property

Hi guys its being awhile I guess its because of so much i most be involved in well but this was short and prompting. Recently i was faced with removing a key from System.properties(), what i found out is that some developer, might try to approach this by setting the value to null, but then System.setProperty(key, val) will throw NullPointerException when either key or value is null. Here are 2 ways to remove or clear a system property:
  1. clearProperty(String key) introduced in JDK 1.5.


  2. System.getProperties().remove("key");
    

When java security manager is enabled, Both methods need specific permissions to succeed. Approach 1 requires PropertyPermission "key", "read,write". Approach 2 requires a much wider permission: PropertyPermission "*", "read,write", because you are free to modify/remove all system properties with the returned object from System.getProperties()