Thursday, March 20, 2008

Java : Parsing XML

So there I was needing to parse some XML in a Java program and not wanting to refresh my memory of the Java classes to do this.

Had a look around on the Internet and found JFig

This allows you to set up XML files and then read the parameters using a construct like:

JFig.getInstance().getValue(sectionName, key)

So for XML like:


#section name="Section1"*
#entry key="key1" value="val1" /*
#/section*

use:

JFig.getInstance().getValue("Section1", "key1")

Neat - and saves me doing the parsing.

But then I discovered some other neat features e.g. you have have a hierarchy of XML files and some "inherit" others.

Also, you have have substitution values e.g.

#entry key="key2" value="[Section1]{key1}" /*


which allows you to define some "constants" at the beginning of the XML file and then JFig will replicate them through for you.

... where # = "<" and * = ">"

Very neat!

Useful article here. Another solution is to use Jakarta Commons Configuration but JFig is much simpler.

Enjoy!

Monday, October 29, 2007

Java : Creating a date

The old format was

Date date = new Date ("2007/10/29") but that has long been deprecated.

Use this:



DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date date = dfm.parse("1990-01-01");
}

catch (Exception e)
{
System.out.println("Date exception is: " + e.getMessage());
e.printStackTrace();
}



If you wanted to add the time:



DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try
{
Date date = dfm.parse("1990-01-01 12:40:00");
}

catch (Exception e)
{
System.out.println("Date exception is: " + e.getMessage());
e.printStackTrace();
}



Enjoy!

Thursday, September 06, 2007

Java : Disassembler

The standard javap will do this for you.

javap -help shows all the commands.

For class x, javap - c x (Note: Don't type x.class - just type x) will decompile the class for you and display methods like:

public int getUserType();
public void setUserType(int);

Enjoy!

Wednesday, August 15, 2007

Eclipse : Matching brackets

Clicking to the right of a bracket shows the matching one in a "box".

To toggle between the two, use Cntl / Shift / P.

Enjoy!

Eclipse : Running command line to EJB inside Eclipse

I'm running Eclipse Europa (the J2EE package).

See the previous post about running a command line Java program inside a DOS box and the million paths etc. you have to set up.

It's a lot easier to run it inside Eclipse by right clicking the application in the Package Explorer and selecting "Run As" / "Java Application".

(Note : JBoss needs to be started first by selecting the Server view inside the J2EE perspective , right click, "Start".)

You get this error in the console:

Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial


As per the previous post, the solution then was to add a path to the jndi.properties file:

set CLASSPATH=C:\JBoss-4.0.5.GA\server\default\conf;%CLASSPATH%

In Eclipse, however, go "Project Properties" / "Run/Debug Settings" / select the launch configuration / "Edit" / click on the "Classpath" tab / "Advanced: / "Add External Folder" and then browse to the jndi.properties folder as above and "OK" out.

That's it - Eclipse takes care of all the other stuff!

Enjoy!

Tuesday, August 14, 2007

Java : Find which class is in which jar file

It's a real pain!

You keep getting ClassNotDefined and you have hundreds of Jar files and it takes ages to manually search them all.

The solution is to use Jarscan.

There's also a web version on the same page.

So e.g.

java -jar jarscan.jar -dir C:\JBoss-4.0.5.GA -class SafeClone

returns (for JBoss):

searching these jarfiles now ....

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jboss-serialization.jar
Library Path: C:\JBoss-4.0.5.GA\client\jboss-serialization.jar
===============================================

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jbossall-client.jar
Library Path: C:\JBoss-4.0.5.GA\client\jbossall-client.jar
===============================================

===============================================
Found: SafeClone
Class: org.jboss.serial.objectmetamodel.safecloning.SafeClone
Package: org.jboss.serial.objectmetamodel.safecloning
Library Name: jboss-serialization.jar
Library Path: C:\JBoss-4.0.5.GA\server\default\lib\jboss-serialization.jar
===============================================

Search took: 10360 milliseconds.


Neat!

Enjoy!

EJB : Running a command line client to a JBoss EJB

The EJB example running on JBoss is taken from the WTP (Web Tools Project) tutorials here plus XDoclet - all inside Eclipse.

Call the package ejbs.

We have an EJBTestBean:


/**
*
*
* @ejb.interface-method view-type="remote"
*
* @generated
*
* //TODO: Must provide implementation for bean method stub
*/
public String greet(String param) {
return "Hi dude " + param;
}


XDoclet creates all the Home and Util stubs.

Now we want to test this from a Java command line client.

So we create a Java class with a main() method (called CLTestClient):


try
{
EJBTestHome home = ejbs.EJBTestUtil.getHome();
EJBTest service = home.create();
String result = service.greet("Tom");
System.out.println();
System.out.println("Result is " + result);
}

catch (Exception e)
{
System.out.println();
System.out.println("Exception " + e.getMessage());
}


and we want to test from the command line:

java CLTestClient

and we get ClassNotDefined all over the place.

So we add the EJB files to the CLASSPATH:

set CLASSPATH=C:\JBoss-4.0.5.GA\
server\default\lib\jboss-j2ee.jar;C:\your path\EJBTestClient\build\classes;C:\your path\EJBTestClient\build\classes\ejbs;%CLASSPATH%

Then we get this error:

Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial


The solution is to add a path to the jndi.properties file:

set CLASSPATH=C:\JBoss-4.0.5.GA\server\default\conf;%CLASSPATH%

More ClassNotDefined errors:

set CLASSPATH=C:\JBoss-4.0.5.GA\client\jbossall-client.jar;C:\JBoss-4.0.5.GA\lib\jboss-common.jar;C:\JBoss-4.0.5.GA\server\default\lib\jboss.jar;C:\JBoss-4.0.5.GA\client\jnp-client.jar;C:\JBoss-4.0.5.GA\client\jboss-client.jar;%CLASSPATH%

and finally:

java CLTestClient

returns:

Result is Hi dude Tom

By the way, if you wanted to do this from a browser inside a jsp page:


LT%@page import="ejbs.EJBTestUtil"%GT
LThtmlGT
LTheadGT
LTtitleGTEJBWebTestLT/titleGT
LT/headGT
LTbodyGT
LT%
ejbs.EJBTest tb = null;

try
{
ejbs.EJBTestHome home = EJBTestUtil.getHome();
tb = home.create();
}

catch(Exception exception)
{

}

%GT

LTbGTLT%= tb.greet("Tom") %GTLT/bGT

LT/bodyGT
LT/htmlGT




Where LT = "less than" and GT = "greater than".

Enjoy!

Tuesday, July 24, 2007

Visual Studio : Using a xsd to validate / create xml

With VS 2005, there's a neat feature which allows you to validate / check a xml document based on a xsd.

Load the xml document (or the bare bones) and then click the "Properties" tab on the RHS (where "Solution Explorer" is). You'll see a "schema" entry. Point this to the xsd that describes the xml.

You might need to use "file:// ... " if the file is on your hard drive.

Now you will see that all the errors are highlighted in the usual way and also Intelli-sense guides you as you build up the xml.

Enjoy!

Wednesday, July 18, 2007

Misc : Cracking XP Administrator passwords

Check out this site:

http://www.loginrecovery.com/

"Login Recovery is a service to reveal user names and recover passwords for Windows NT, 2000, XP, 2003 and Vista. As long as you have physical access to the computer, your passwords can be recovered

By following three simple steps, over 98.5% of passwords can be recovered within less than ten minutes. This service does not overwrite passwords, it does not write anything to the hard drive, it does not alter the computer in any way.

For immediate access to decrypted passwords, the priority service is available for GBP 12.95 + VAT (£15.22) (approx USD 25 or EUR 20). Alternatively a free service is available by waiting up to 48 hours (One free request every three months)."


Works like a charm as long as you don't mind waiting 48 hours!

Enjoy!

Thursday, July 05, 2007

DOS 'grep' equivalent - the find command

The previous post talked about finding which ports were open using the netstat command.

But you still have to scan the output manually to find the port you are looking for.

In the Unix world you could simply use grep and pipe the output to it.

In the DOS world, you can use find.


help find

Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
[drive:][path]filename
Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.



So to find if port 86 was open, use:

netstat -ano | find /i "86"

and the filtered output would something like:



TCP 0.0.0.0:86 0.0.0.0:0 LISTENING 728
UDP 0.0.0.0:86 *:* 728



Enjoy!

Which XP ports are open?

Don't know how many times I see the message "Error - port 8080 already in use" or some such.

So how do you find whose using that port. One answer is netstat which runs in a cmd window:


Displays protocol statistics and current TCP/IP network connections.

NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]

-a Displays all connections and listening ports.
-b Displays the executable involved in creating each connection or
listening port. In some cases well-known executables host
multiple independent components, and in these cases the
sequence of components involved in creating the connection
or listening port is displayed. In this case the executable
name is in [] at the bottom, on top is the component it called,
and so forth until TCP/IP was reached. Note that this option
can be time-consuming and will fail unless you have sufficient
permissions.
-e Displays Ethernet statistics. This may be combined with the -s
option.
-n Displays addresses and port numbers in numerical form.
-o Displays the owning process ID associated with each connection.
-p proto Shows connections for the protocol specified by proto; proto
may be any of: TCP, UDP, TCPv6, or UDPv6. If used with the -s
option to display per-protocol statistics, proto may be any of:
IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, or UDPv6.
-r Displays the routing table.
-s Displays per-protocol statistics. By default, statistics are
shown for IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, and UDPv6;
the -p option may be used to specify a subset of the default.
-v When used in conjunction with -b, will display sequence of
components involved in creating the connection or listening
port for all executables.
interval Redisplays selected statistics, pausing interval seconds
between each display. Press CTRL+C to stop redisplaying
statistics. If omitted, netstat will print the current
configuration information once.



so netstat -ano gives



Active Connections

Proto Local Address Foreign Address State PID
TCP 0.0.0.0:86 0.0.0.0:0 LISTENING 728
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1192
TCP 0.0.0.0:261 0.0.0.0:0 LISTENING 3684
...





You can look up the PID using Task Manager or (even better) using sysinternals Process Explorer (which you can get from here).

Their version of netstat is TCPView (which you can get from here).

Enjoy!

Friday, June 01, 2007

Unix : DOS format on a Unix box

Quite often I use an IDE on Windows to edit the file and then FTP the file to a Linux box. (Mainly because vi sucks major league, big time!)

Anyway, the Unix compiler sometimes complains about rogue characters. An easy way to fix this is to use the

dos2unix

utility which reads the text file and converts to Unix format.

Enjoy!