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!