Stivlo'st in Asia

Programming and Travel

Browsing Posts in Java

I’ve some troubles editing large files (over 1Mb) with Netbeans: editing gets slower and freezes for many seconds. The IDE Faq actually says: “The IDE automatically determines optimal Java VM memory settings based on the size of physical memory. Rarely you might want to change the heap size.”

Since the performance are disappointed I want to give a try anyway. In my netbeans.conf I had the following line:

netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true"

Which I changed to:

netbeans_default_options="-J-client -J-Xss2m -J-Xms256m -J-Xmx1024m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true -J-XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled"

The main changes are -J-Xms256m which tells Netbeans to start with a heap of 256Mb and -J-Xmx1024m which is the maximum heap size (1024Mb).

The bottom line: I didn’t have a performance boost and I decided to edit the very big files with Scite which does a much better job.

Java cleanup error

No comments

I was reviewing my own code and I found this in the finally clause of a db application:

} finally {
    try {
        if (res != null) res.close();
        if (stmt != null) stmt.close();
    } catch (SQLException ex) {
        throw ex;
    }
}

In other words if res.close() throws and exception, line 4 is not executed and so the statement doesn’t get closed! It’s quite subtle because finally statements are already annoying and by writing them in this way I didn’t see the problem. A better way to implement this is to write two close methods that actually ignore the error.

private static void close(ResultSet rs) {
    if (rs==null) return;
    try {
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
} 

private static void close(Statement sm) {
    if (sm==null) return;
    try {
        sm.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

So the  finally clause now looks cleaner, shorter and actually works!

} finally {
    close(stmt);
    close(res);
}

Previously I blogged about installing Mod_jk with Tomcat 5.5 on Linux. This time I’m going to install Mod_jk aka Tomcat Connector on Windows. The binary version can be downloaded here:

http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/

I downloaded mod_jk-1.2.28-httpd-2.2.3.so – renamed to mod_jk.so and copied to

C:\wamp\bin\apache\Apache2.2.11\modules

I create  C:\wamp\bin\apache\Apache2.2.11\conf\workers.properties with the following content

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8010 #Check your ajp13 port!

All files in C:\wamp\alias are included automatically, so even if they are actually meant for aliases I will place my mod_jk configuration there.

I create a file c:\wamp\alias\mod_jk.conf with the following contents

# Load mod_jk module
LoadModule jk_module C:\wamp\bin\apache\Apache2.2.11\modules\mod_jk.so

# Where to find workers.properties
JkWorkersFile C:\wamp\bin\apache\Apache2.2.11\conf\workers.properties

# Where to put jk shared memory
JkShmFile C:\wamp\logs\mod_jk.shm

# Where to put jk logs
JkLogFile C:\wamp\logs\mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the timestamp log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] “

Finally I add the JkMounts I need in one of virtualhost configuration:

JkMount /kines_api/* worker1

JAX-WS 2.1 second day

No comments

Today I tried again Netbeans 6.5 and magically this suggestion appeared

**************************************WARNING***************************************
To use JAX-WS 2.1 with JDK 6, newer versions of API jars need to be in bootclasspath
before rt.jar. To do this you can use Java endorsed mechanism to override what is in
Java platform. The easiest way to do so is to copy all jars from
C:\Program Files\NetBeans65\java2\modules\ext\jaxws21\api and
C:\Program Files\NetBeans65\ide10\modules\ext\jaxb\api folders to
C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.18\endorsed.
************************************************************************************
It was a good suggestion, after restarting Netbeans everything worked. Normally I like my job, but sometimes it’s frustrating when you’re stuck like that. Good that it’s finally solved.

Here is how I have lost a lot of time for a stupid error.I could not compile any more my webservice project because of the following error.

wsgen-init:
wsgen-KinesService:
C:\Users\stefano\Documents\NetBeansProjects\kines_api\nbproject\jaxws-build.xml:18: Error starting wsgen:
BUILD FAILED (total time: 6 seconds)

According to this article only jaxb-api.jar and jaxws-api.jar have to be copied, but this approach didn’t work for me! I don’t know why.  I hope some good soul will tell me why..

So I started doing what the article said to avoid.. I created an endorsed directory  in my jdk1.6.0\jre\lib and I copied jaxws-tools.jar and restarted NetBeans.

After restarting I had this other message

wsgen-init:
C:\Users\stefano\Documents\NetBeansProjects\kines_api\nbproject\jaxws-build.xml:13: taskdef A class needed by class com.sun.tools.ws.ant.WsGen cannot be found: com/sun/istack/tools/ProtectedTask
BUILD FAILED (total time: 0 seconds)
I tried to find the class in the jaxws21 directory, using xargs and grepping for two patterns
$ ls *.jar | xargs -l1 unzip -l | grep -e jar -e ProtectedTask
Archive:  FastInfoset.jar
Archive:  activation.jar
Archive:  http.jar
Archive:  jaxb-api.jar
Archive:  jaxb-impl.jar
Archive:  jaxb-xjc.jar
2849  02-25-08 15:04   com/sun/istack/tools/ProtectedTask$AntElement.class
2810  02-25-08 15:04   com/sun/istack/tools/ProtectedTask.class
Archive:  jaxws-api.jar
Archive:  jaxws-rt.jar
Archive:  jaxws-tools.jar
Archive:  jsr173_api.jar
Archive:  jsr181-api.jar
Archive:  jsr250-api.jar
Archive:  mimepull.jar
Archive:  resolver.jar
Archive:  saaj-api.jar
Archive:  saaj-impl.jar
Archive:  sjsxp.jar
Archive:  stax-ex.jar
Archive:  streambuffer.jar
I then proceded to copy jaxb-xjc.jar to endorsed dir and restart the IDE again.
wsgen-init:
C:\Users\stefano\Documents\NetBeansProjects\kines_api\nbproject\jaxws-build.xml:13: taskdef A class needed by class com.sun.tools.ws.ant.WsGen cannot be found: org/apache/tools/ant/DynamicConfigurator
BUILD FAILED (total time: 0 seconds)

So I copied ant.jar to endorsed.. sigh.. and restarted the IDE again.

java.lang.ClassCastException: org.apache.tools.ant.helper.DefaultExecutor cannot be cast to org.apache.tools.ant.Executor
at org.apache.tools.ant.Project.getExecutor(Project.java:1174)
at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:273)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:499)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
BUILD FAILED (total time: 0 seconds)

I could find an article saying it’s enough to copy only jaxb-api.jar – I tried but it didn’t work either. At this point I decided to install again NetBeans 6.1 … but only to get this error:

wsgen-KinesService:
Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext
at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:158)
at com.sun.xml.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:87)
at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:262)
at com.sun.tools.ws.wscompile.WsgenTool.buildModel(WsgenTool.java:218)
at com.sun.tools.ws.wscompile.WsgenTool.run(WsgenTool.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.tools.ws.Invoker.invoke(Invoker.java:116)
at com.sun.tools.ws.WsGen.main(WsGen.java:52)
Caused by: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.lang.StackTraceElement does not have a no-arg default constructor.
this problem is related to the following location:
at java.lang.StackTraceElement
at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()
at java.lang.Throwable
at java.lang.Exception
at java.sql.SQLException
at private java.sql.SQLException it.kines.ws.jaxws.SQLExceptionBean.nextException
at it.kines.ws.jaxws.SQLExceptionBean

at java.security.AccessController.doPrivileged(Native Method)
at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:148)
... 10 more
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.lang.StackTraceElement does not have a no-arg default constructor.
this problem is related to the following location:
at java.lang.StackTraceElement
at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()
at java.lang.Throwable
at java.lang.Exception
at java.sql.SQLException
at private java.sql.SQLException it.kines.ws.jaxws.SQLExceptionBean.nextException
at it.kines.ws.jaxws.SQLExceptionBean
at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:438)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:286)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:139)
at com.sun.xml.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:105)
at com.sun.xml.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:153)
at com.sun.xml.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:149)
... 12 more
Command invoked: wsgen "C:\Program Files\Java\jdk1.6.0\jre\bin\java.exe" "-Djava.endorsed.dirs=C:\Program Files\NetBeans 6.1\java2\modules\ext\jaxws21\api" -classpath "C:\Program Files\Java\jdk1.6.0\lib\tools.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\build\web\WEB-INF\classes;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\activation.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxb-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxb-impl.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxb-xjc.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jsr173_api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\FastInfoset.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\http.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxws-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxws-rt.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jsr181-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jsr250-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\saaj-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\saaj-impl.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\sjsxp.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\resolver.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\stax-ex.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\streambuffer.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\jaxws-tools.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxws21\mimepull.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\classes;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\commons-email-1.0.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\commons-validator-1.3.1.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jakarta-oro-2.0.8.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jfig-1.5.2.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\log4j-1.2.14.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\mailapi.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\mysql-connector-java-5.0.5-bin.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\smtp.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxb\activation.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxb\jaxb-api.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxb\jaxb-impl.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxb\jaxb-xjc.jar;C:\Users\stefano\Documents\NetBeansProjects\kines_api\lib\jaxb\jsr173_api.jar" com.sun.tools.ws.WsGen -d C:\Users\stefano\Documents\NetBeansProjects\kines_api\build\generated\wsgen\binaries -keep -wsdl -r C:\Users\stefano\Documents\NetBeansProjects\kines_api\build\generated\wsgen\service\resources -s C:\Users\stefano\Documents\NetBeansProjects\kines_api\build\generated\wsgen\service it.kines.ws.KinesService
C:\Users\stefano\Documents\NetBeansProjects\kines_api\nbproject\jaxws-build.xml:18: wsgen failed
BUILD FAILED (total time: 14 seconds)

But this error was not connected to the issue, but it was caused by a new webmethod which threw a SQLException. After I took care of that exception removing the throw statement the compilation worked!

Despite that, I had a runtime error, but this time from Tomcat, a whole different environment.

GRAVE: WSSERVLET11: failed to parse runtime descriptor: java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/C:/Users/stefano/Documents/NetBeansProjects/kines_api/build/web/WEB-INF/lib/jaxb-impl.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)
java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/C:/Users/stefano/Documents/NetBeansProjects/kines_api/build/web/WEB-INF/lib/jaxb-impl.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)

This could be solved by copying jaxb-api.jar into Apache Tomcat 6.0.16\lib\shared and setting conf/catalina.properties shared_loader pointing to that directory.

The problems were not over! After starting tomcat, this time I had a class not found problem, despite that class is included in the war file.

26-ott-2008 23.17.03 com.sun.xml.ws.transport.http.servlet.WSServletContextListener contextInitialized
GRAVE: WSSERVLET11: failed to parse runtime descriptor: java.lang.NoClassDefFoundError: com/obliquid/util/HasDb
java.lang.NoClassDefFoundError: com/obliquid/util/HasDb
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
Powered by WordPress Web Design by SRS Solutions © 2010 Stivlo'st in Asia Design by SRS Solutions