How can I update Websphere 7 to use EL2.2? - java

This is what I have done:
Following this post from Lincoln Baxter I downloaded el-api-2.2.jar and el-impl-2.2.jar
Then created isolated shard library and added them there.
We are using Myfaces so in web.xml added this bit
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
This is the issue:
I do not think it getting picked up and application is still using one out the the box apache based.
Caused by: javax.el.ELException: Error Parsing: #{testBean.testel2('hello')}
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:129)
at org.apache.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:150)
at org.apache.el.lang.ExpressionBuilder.createMethodExpression(ExpressionBuilder.java:201)
at org.apache.el.ExpressionFactoryImpl.createMethodExpression(ExpressionFactoryImpl.java:56)
at org.apache.myfaces.view.facelets.tag.TagAttributeImpl.getMethodExpression(TagAttributeImpl.java:219)
at org.apache.myfaces.view.facelets.tag.jsf.ActionSourceRule$ActionMapper2.applyMetadata(ActionSourceRule.java:73)
at org.apache.myfaces.view.facelets.tag.MetadataImpl.applyMetadata(MetadataImpl.java:45)
at javax.faces.view.facelets.MetaTagHandler.setAttributes(MetaTagHandler.java:68)
at javax.faces.view.facelets.DelegatingMetaTagHandler.setAttributes(DelegatingMetaTagHandler.java:93)
at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.setAttributes(BehaviorsAddingComponentHandlerWrapper.java:115)
at org.apache.myfaces.view.facelets.tag.jsf.ComponentTagHandlerDelegate.apply(ComponentTagHandlerDelegate.java:269)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:54)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:51)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:59)
at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:55)
at org.apache.myfaces.view.facelets.tag.jsf.ComponentTagHandlerDelegate.apply(ComponentTagHandlerDelegate.java:324)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:54)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:59)
at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:55)
at org.apache.myfaces.view.facelets.tag.jsf.ComponentTagHandlerDelegate.apply(ComponentTagHandlerDelegate.java:324)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:54)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:51)
at org.apache.myfaces.view.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:86)
at org.apache.myfaces.view.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:167)
at org.apache.myfaces.view.facelets.impl.TemplateContextImpl$TemplateManagerImpl.apply(TemplateContextImpl.java:128)
at org.apache.myfaces.view.facelets.impl.TemplateContextImpl.includeDefinition(TemplateContextImpl.java:92)
at org.apache.myfaces.view.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:433)
at org.apache.myfaces.view.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:93)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:51)
at org.apache.myfaces.view.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:57)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:51)
at org.apache.myfaces.view.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:45)
at org.apache.myfaces.view.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:322)
at org.apache.myfaces.view.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:369)
at org.apache.myfaces.view.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:347)
at org.apache.myfaces.view.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:215)
at org.apache.myfaces.view.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:140)
at org.apache.myfaces.view.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:57)
at org.apache.myfaces.view.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:45)
at org.apache.myfaces.view.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:143)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.buildView(FaceletViewDeclarationLanguage.java:363)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:66)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
... 31 more
Caused by: org.apache.el.parser.ParseException: Encountered "(" at line 1, column 26.
Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
"<=" ...
"le" ...
"==" ...
"eq" ...
"!=" ...
"ne" ...
"&&" ...
"and" ...
"||" ...
"or" ...
"*" ...
"+" ...
"-" ...
"/" ...
"div" ...
"%" ...
"mod" ...
at org.apache.el.parser.ELParser.generateParseException(ELParser.java:2142)
at org.apache.el.parser.ELParser.jj_consume_token(ELParser.java:2024)
at org.apache.el.parser.ELParser.DeferredExpression(ELParser.java:113)
at org.apache.el.parser.ELParser.CompositeExpression(ELParser.java:40)
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:97)
Update: I tried to use JBoss EL instead and I notice this line
[13/02/12 15:19:06:113 EST] 00000029 ExternalSpeci I MyFaces Unified EL support disabled
in the log and that led me to this code
Copied from source of myfaces-impl-2.1.6
package org.apache.myfaces.util;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.ELContext;
/**
* <p>
* Utility class for determining which specifications are available
* in the current process. See JIRA issue: http://issues.apache.org/jira/browse/MYFACES-2386
* </p>
*
* #author Jan-Kees van Andel
* #author Jakob Korherr (latest modification by $Author: lu4242 $)
* #version $Revision: 963000 $ $Date: 2010-07-11 01:54:18 -0500 (Sun, 11 Jul 2010) $
* #since 2.0
*/
public final class ExternalSpecifications
{
//private static final Log log = LogFactory.getLog(BeanValidator.class);
private static final Logger log = Logger.getLogger(ExternalSpecifications.class.getName());
private static volatile Boolean beanValidationAvailable;
private static volatile Boolean unifiedELAvailable;
//Removed code from here before posting on Stack Overflow
//Relevant method is below
/**
* This method determines if Unified EL is present.
*
* Eager initialization is used for performance. This means Unified EL binaries
* should not be added at runtime after this variable has been set.
* #return true if UEL is available, false otherwise.
*/
public static boolean isUnifiedELAvailable()
{
if (unifiedELAvailable == null)
{
try
{
// Check if the UEL classes are available.
// If the JSP EL classes are loaded first, UEL will not work
// properly, hence it will be disabled.
unifiedELAvailable = (
Class.forName("javax.el.ValueReference") != null
&& Class.forName("javax.el.ValueExpression")
.getMethod("getValueReference", ELContext.class) != null
);
}
catch (Throwable t)
{
//log.log(Level.FINE, "Error loading class (could be normal)", t);
unifiedELAvailable = false;
}
log.info("MyFaces Unified EL support " + (unifiedELAvailable ? "enabled" : "disabled"));
}
return unifiedELAvailable;
}
/**
* this class should not be instantiated.
*/
private ExternalSpecifications()
{
}
}
try block throws exception on both checks when inspecting and during executing as expected goes to catch and that results in a false being returned. I believe due to this it falls back to EL provided by WebSphere and not JBoss EL.

Related

R wrapper for a java method in a jar using rjava

I am trying to access a java program MELTING 5 in R using the rjava package.
I can do it using the system function as follows using the batch file.
path <- "path/to/melting.bat"
sequence = "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC"
hybridisation.type = "dnadna"
OligomerConc = 5e-8
Sodium = 0.05
command=paste("-S", sequence,
"-H", hybridisation.type,
"-P", OligomerConc,
"-E", paste("Na=", Sodium, sep = ""))
system(paste("melting.bat", command))
I am trying to do the same using a wrapper, following the steps in hellowjavaworld without any success.
.jaddClassPath('path/to/melting5.jar')
main <- .jnew("melting/Main")
out <- .jcall(obj = main, returnSig = "V", method = "main", .jarray(list(), "java/lang/String"),
argument = command)
The java code in melting/Main.java in the melting5.jar that I am trying to access is as follows.
package melting;
import java.text.NumberFormat;
import melting.configuration.OptionManagement;
import melting.configuration.RegisterMethods;
import melting.methodInterfaces.MeltingComputationMethod;
import melting.nearestNeighborModel.NearestNeighborMode;
/**
* The Melting main class which contains the public static void main(String[] args) method.
*/
public class Main {
// private static methods
/**
* Compute the entropy, enthalpy and the melting temperature and display the results.
* #param args : contains the options entered by the user.
* #param OptionManagement optionManager : the OptionManegement which allows to manage
* the different options entered by the user.
*/
private static ThermoResult runMelting(String [] args, OptionManagement optionManager){
try {
ThermoResult results =
getMeltingResults(args, optionManager);
displaysMeltingResults(results);
return results;
} catch (Exception e) {
OptionManagement.logError(e.getMessage());
return null;
}
}
/**
* Compute the entropy, enthalpy and melting temperature, and return
* these results.
* #param args options (entered by the user) that determine the
* sequence, hybridization type and other features of the
* environment.
* #param optionManager the {#link
* melting.configuration.OptionManagement
* <code>OptionManagement</code>} which
* allows the program to manage the different
* options entered by the user.
* #return The results of the Melting computation.
*/
public static ThermoResult getMeltingResults(String[] args,
OptionManagement optionManager)
{
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2);
// Set up the environment from the supplied arguments and get the
// results.
Environment environment = optionManager.createEnvironment(args);
RegisterMethods register = new RegisterMethods();
MeltingComputationMethod calculMethod =
register.getMeltingComputationMethod(environment.getOptions());
ThermoResult results = calculMethod.computesThermodynamics();
results.setCalculMethod(calculMethod);
environment.setResult(results);
// Apply corrections to the results.
results = calculMethod.getRegister().
computeOtherMeltingCorrections(environment);
environment.setResult(results);
return environment.getResult();
}
/**
* displays the results of Melting : the computed enthalpy and entropy (in cal/mol and J/mol), and the computed
* melting temperature (in degrees).
* #param results : the ThermoResult containing the computed enthalpy, entropy and
* melting temperature
* #param MeltingComputationMethod calculMethod : the melting computation method (Approximative or nearest neighbor computation)
*/
private static void displaysMeltingResults(ThermoResult results)
{
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2);
MeltingComputationMethod calculMethod =
results.getCalculMethod();
double enthalpy = results.getEnthalpy();
double entropy = results.getEntropy();
OptionManagement.logInfo("\n The MELTING results are : ");
if (calculMethod instanceof NearestNeighborMode){
OptionManagement.logInfo("Enthalpy : " + format.format(enthalpy) + " cal/mol ( " + format.format(results.getEnergyValueInJ(enthalpy)) + " J /mol)");
OptionManagement.logInfo("Entropy : " + format.format(entropy) + " cal/mol-K ( " + format.format(results.getEnergyValueInJ(entropy)) + " J /mol-K)");
}
OptionManagement.logInfo("Melting temperature : " + format.format(results.getTm()) + " degrees C.\n");
}
// public static main method
/**
* #param args : contains the options entered by the user.
*/
public static void main(String[] args) {
OptionManagement optionManager = new OptionManagement();
if (args.length == 0){
optionManager.initialiseLogger();
optionManager.readMeltingHelp();
}
else if (optionManager.isMeltingInformationOption(args)){
try {
optionManager.readOptions(args);
} catch (Exception e) {
OptionManagement.logError(e.getMessage());
}
}
else {
runMelting(args, optionManager);
}
}
}
How to pass arguments in command to public static void main in java jar?
Over at https://github.com/hrbrmstr/melting5jars I made a pkg wrapper for the MELTING 5 jar (melting5.jar) and also put the Data/ directory in it so you don't have to deal with jar-file management. It can be installed via devtools::install_github("hrbrmstr/melting5jars"),
BEFORE you load that library, you need to set the NN_PATH since the Data/ dir is not where the jar expects it to be by default and you may run into issues setting it afterwards (YMMV).
NOTE: I don't work with this Java library and am not in your field, so please double check the results with the command-line you're used to running!
So, the first things to do to try to get this to work are:
Sys.setenv("NN_PATH"=system.file("extdata", "Data", package="melting5jars"))
library(melting5jars) # devtools::install_github("hrbrmstr/melting5jars")
Now, one of the cooler parts of rJava is that you get to work in R (code) if you want to vs Java (code). We can recreate the core parts of that Main class right in R.
First, get a new melting.Main object and a new OptionManagement object just like the Java code does:
melting <- new(J("melting.Main"))
optionManager <- new(J("melting.configuration.OptionManagement"))
Next, we setup your options. I left Sodium the way it is just to ensure I didn't mess anything up.
Sodium <- 0.05
opts <- c(
"-S", "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC",
"-H", "dnadna",
"-P", 5e-8,
"-E", paste("Na=", Sodium, sep = "")
)
Now, we can call getMeltingResults() from that Main class directly:
results <- melting$getMeltingResults(opts, optionManager)
and then perform the same calls on those results:
calculMethod <- results$getCalculMethod()
enthalpy <- results$getEnthalpy()
entropy <- results$getEntropy()
if (.jinstanceof(calculMethod, J("melting.nearestNeighborModel.NearestNeighborMode"))) {
enthalpy <- results$getEnergyValueInJ(enthalpy)
entropy <- results$getEnergyValueInJ(entropy)
}
melting_temperature <- results$getTm()
enthalpy
## [1] -1705440
entropy
## [1] -4566.232
melting_temperature
## [1] 72.04301
We can wrap all that up into a function that will make it easier to call in the future:
get_melting_results <- function(opts = c()) {
stopifnot(length(opts) > 2) # a sanity check that could be improved
Sys.setenv("NN_PATH"=system.file("extdata", "Data", package="melting5jars"))
require(melting5jars)
melting <- new(J("melting.Main"))
optionManager <- new(J("melting.configuration.OptionManagement"))
results <- melting$getMeltingResults(opts, optionManager)
calculMethod <- results$getCalculMethod()
enthalpy_cal <- results$getEnthalpy()
entropy_cal <- results$getEntropy()
enthalpy_J <- entropy_J <- NULL
if (.jinstanceof(calculMethod, J("melting.nearestNeighborModel.NearestNeighborMode"))) {
enthalpy_J <- results$getEnergyValueInJ(enthalpy_cal)
entropy_J <- results$getEnergyValueInJ(entropy_cal)
}
melting_temp_C <- results$getTm()
list(
enthalpy_cal = enthalpy_cal,
entropy_cal = entropy_cal,
enthalpy_J = enthalpy_J,
entropy_J = entropy_J,
melting_temp_C = melting_temp_C
) -> out
class(out) <- c("melting_res")
out
}
That also has separate values for enthalpy and entropy depending on the method result.
We can also make a print helper function since we classed the list() we're returning:
print.melting_res <- function(x, ...) {
cat(
"The MELTING results are:\n\n",
" - Enthalpy: ", prettyNum(x$enthalpy_cal), " cal/mol",
{if (!is.null(x$enthalpy_J)) paste0(" (", prettyNum(x$enthalpy_J), " J /mol)", collapse="") else ""}, "\n",
" - Entropy: ", prettyNum(x$entropy_cal), " cal/mol-K",
{if (!is.null(x$entropy_J)) paste0(" (", prettyNum(x$entropy_J), " J /mol-K)", collapse="") else ""}, "\n",
" - Meltng temperature: ", prettyNum(x$melting_temp_C), " degress C\n",
sep=""
)
}
(I made an assumption you're used to seeing the MELTING 5 command line output)
And, finally, re-run the computation:
Sodium <- 0.05
opts <- c(
"-S", "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC",
"-H", "dnadna",
"-P", 5e-8,
"-E", paste("Na=", Sodium, sep = "")
)
res <- get_melting_results(opts)
res
## The MELTING results are:
##
## - Enthalpy: -408000 cal/mol (-1705440 J /mol)
## - Entropy: -1092.4 cal/mol-K (-4566.232 J /mol-K)
## - Meltng temperature: 72.04301 degress C
str(res)
## List of 5
## $ enthalpy_cal : num -408000
## $ entropy_cal : num -1092
## $ enthalpy_J : num -1705440
## $ entropy_J : num -4566
## $ melting_temp_C: num 72
## - attr(*, "class")= chr "melting_res"
You should be able to use the above methodology to wrap other components (if any) in the MELTING library.

Cannot find exception's cause [duplicate]

I made a color palette with a jPanel and a JLabel array in it. At first it worked well, but then i put some other jLabels out of the JPanel and added them some events. Now I keep getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:435)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515)
at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:380)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:116)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
I tried to remove everything i've done after first time i got this error, but still keep getting it. When i change the layout from GridLayout to anything else, then the error disappears, but the code becomes useless. So i need GridLayout. When i move everything in that JPanel to another JPanel, the error also goes away. But when i remove the first JPanel, error comes back.
By the way, the program works, but it's not pleasent to keep getting errors...
Edit: When i use less than 225 color, there's no error. I'm really curious about what's happening. Any explanation would be appreciated...
It seems to me like you've hit a bug in the JDK since the error seems to come from Swing classes.
Options:
Define the property java.util.Arrays.useLegacyMergeSort as true. Either using in your code the line
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
before any Swing code. As the first line in the main method should work.
Or adding
-Djava.util.Arrays.useLegacyMergeSort=true
to your starting options (in the console, or in the project properties in an IDE, Ant script, etc.)
Upgrade your JDK and see if the problem goes away
Downgrade to Java 6
Report my findings:
-Djava.util.Arrays.useLegacyMergeSort=true
works
but
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
does not work.
It is due to the fact that in JDK Arrays.class
static final class LegacyMergeSort {
private static final boolean userRequested = ...
It is a static variable which is defined when jvm starts. Setting System property in the program will have no effect if the class has been loaded into jvm.
I have beeing monitoring the LegacyMergeSort.userRequested variable, and the findings confirmed with above statement.
Update:
The program must set system properties before java.util.Arrays is loaded to classloader.
Otherwise, once it is loaded, setting the properties is not going to be useful due to the reason mentioned above.
Make sure nothing else loaded Arrays.class:
By putting following code to your program to test:
java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[] { String.class });
m.setAccessible(true);
ClassLoader cl = ClassLoader.getSystemClassLoader();
Object test1 = m.invoke(cl, "java.util.Arrays");
System.out.println("test1 loaded? ->" + (test1 != null));
[Update]
This solution unfortunately is not guaranteed to solve the problem in all cases. It is not enough to patch the default SortingFocusTraversalPolicy
of the KeyboardFocusManager.
I recommend to read the answer by Robin Loxley below, including his Update.
[/Update]
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:868)
This problem is caused by a bug in javax.swing.LayoutComparator.
The following class installs a fixed version of javax.swing.LayoutComparator, which does not violate the contract of Comparator<Component>. This (or any other) fixed version of javax.swing.LayoutComparator should be submitted to Oracle by some Oracle contributor.
package ...;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.FocusTraversalPolicy;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.JRootPane;
import javax.swing.SortingFocusTraversalPolicy;
import javax.swing.UIManager;
/**
* Uses reflection to install a fixed version of {#link javax.swing.LayoutComparator} to solve the
* LayoutFocusTraversalPolicy/TimSort problem.
*
* <p>
* <code>java.lang.IllegalArgumentException: Comparison method violates its general contract!</code>
* <br/>
* {#code at java.util.TimSort.mergeHi(TimSort.java:868)}
* </p>
* <p>
* Usage: call {#code Class.forName(LayoutFocusTraversalPolicyTimSortBugFixer.class.getName())}
* before creating Swing components.
* </p>
*
* #author Burkhard Strauss
* #since Feb 2015
*/
public class LayoutFocusTraversalPolicyTimSortBugFixer
{
static
{
UIManager.getUI(new JRootPane()); // make Swing install the SortingFocusTraversalPolicy
final KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager
.getCurrentKeyboardFocusManager();
final FocusTraversalPolicy focusTraversalPolicy = keyboardFocusManager
.getDefaultFocusTraversalPolicy();
boolean fixed = false;
if (focusTraversalPolicy instanceof SortingFocusTraversalPolicy)
{
try
{
final Field field = SortingFocusTraversalPolicy.class.getDeclaredField("comparator");
final boolean accessible = field.isAccessible();
try
{
field.setAccessible(true);
field.set(focusTraversalPolicy, new LayoutComparator());
fixed = true;
}
finally
{
field.setAccessible(accessible);
}
}
catch (final Exception e)
{
}
}
if (!fixed)
{
Loggers.getLoggerFor(LayoutFocusTraversalPolicyTimSortBugFixer.class).warn("could not fix the bug");
}
}
/**
* Fixed version of {#link javax.swing.LayoutComparator}.
* <p>
* Search for 'bugfix' in the code.
* </p>
*
* #author Burkhard Strauss
* #since Feb 2015
*/
#SuppressWarnings("serial")
private static class LayoutComparator implements Comparator<Component>, java.io.Serializable
{
private static final int ROW_TOLERANCE = 10;
private boolean horizontal = true;
private boolean leftToRight = true;
#SuppressWarnings("unused")
void setComponentOrientation(final ComponentOrientation orientation)
{
horizontal = orientation.isHorizontal();
leftToRight = orientation.isLeftToRight();
}
#Override
public int compare(Component a, Component b)
{
if (a == b)
{
return 0;
}
// Row/Column algorithm only applies to siblings. If 'a' and 'b'
// aren't siblings, then we need to find their most inferior
// ancestors which share a parent. Compute the ancestory lists for
// each Component and then search from the Window down until the
// hierarchy branches.
if (a.getParent() != b.getParent())
{
final LinkedList<Component> aAncestory = new LinkedList<Component>();
for (; a != null; a = a.getParent())
{
aAncestory.add(a);
if (a instanceof Window)
{
break;
}
}
if (a == null)
{
// 'a' is not part of a Window hierarchy. Can't cope.
throw new ClassCastException();
}
final LinkedList<Component> bAncestory = new LinkedList<Component>();
for (; b != null; b = b.getParent())
{
bAncestory.add(b);
if (b instanceof Window)
{
break;
}
}
if (b == null)
{
// 'b' is not part of a Window hierarchy. Can't cope.
throw new ClassCastException();
}
for (ListIterator<Component> aIter = aAncestory.listIterator(aAncestory.size()), bIter = bAncestory
.listIterator(bAncestory.size());;)
{
if (aIter.hasPrevious())
{
a = aIter.previous();
}
else
{
// a is an ancestor of b
return -1;
}
if (bIter.hasPrevious())
{
b = bIter.previous();
}
else
{
// b is an ancestor of a
return 1;
}
if (a != b)
{
break;
}
}
}
final int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
{
//
// Here is the bugfix:
// Don't return 0 if a != b. This would violate the contract of
// Comparator<Component>.compare().
//
if (zOrder == 0)
{
zOrder = -1;
}
}
if (horizontal)
{
if (leftToRight)
{
// LT - Western Europe (optional for Japanese, Chinese, Korean)
if (Math.abs(ay - by) < ROW_TOLERANCE)
{
return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
}
else
{
return (ay < by) ? -1 : 1;
}
}
else
{ // !leftToRight
// RT - Middle East (Arabic, Hebrew)
if (Math.abs(ay - by) < ROW_TOLERANCE)
{
return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
}
else
{
return (ay < by) ? -1 : 1;
}
}
}
else
{ // !horizontal
if (leftToRight)
{
// TL - Mongolian
if (Math.abs(ax - bx) < ROW_TOLERANCE)
{
return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
}
else
{
return (ax < bx) ? -1 : 1;
}
}
else
{ // !leftToRight
// TR - Japanese, Chinese, Korean
if (Math.abs(ax - bx) < ROW_TOLERANCE)
{
return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
}
else
{
return (ax > bx) ? -1 : 1;
}
}
}
}
}
}
I just ran into the same error and spent a good amount of time tracking it down. To help others who run into this error it is important to know how to test TimSort. The checks that violate the transitivity contract and throw this error are deep in the algorithm and require a test to meet certain criteria before this problem can be reproduced.
Create a list with 32 or more objects.
Within that list, there needs to two or more runs.
Each run must contain 3 or more objects.
Once you meet those two criteria you can begin testing for this failure.
A run is defined as a sub-set of the list where each item is already in the desired ordered state.
It is not enough to patch LayoutComparator as suggerested above. This fix does not work in my case.
The issue was fixed in JDK 8 (8u45 at least).
SortingFocusTraversalPolicy to use legacy Merge Sort Method.
There is nothing wrong with the JDK.
I was facing the same issue since 2 days & finally got to know that bug was with my date-format.
In my API response few dates were in "dd-MM-yyyy HH:mm" format while few of them were in "dd/MM/yyyy HH:mm" format.
Same issue while comparing integers, May be your List is having some null values.
Here is my code, which is working like a charm
Collections.sort(root_array, new Comparator<RootResponseItem>(){
public int compare(RootResponseItem o1, RootResponseItem o2){
Date date1 = new Date();
String dtStart = o1.getSectors().get(0).getDeparture().getDate() + " " + o1.getSectors().get(0).getDeparture().getTime();
dtStart = dtStart.replaceAll("-","/");
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date1 = format.parse(dtStart);
} catch (ParseException e) {
e.printStackTrace();
}
Date date2 = new Date();
String dtStart2 = o2.getSectors().get(0).getDeparture().getDate() + " " + o2.getSectors().get(0).getDeparture().getTime();
dtStart2 = dtStart2.replaceAll("-","/");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date2 = format2.parse(dtStart2);
} catch (ParseException e) {
e.printStackTrace();
}
return date1.compareTo(date2);
}
});

Using Hapi on an Axis service: "Can't create XML document"

I'm developing an Axis service to interface with a HL7 remote service. I have a class (Hl7MessageTranslator) to encode query request like this:
public Hl7MessageTranslator(...)
{
...
parser = new DefaultXMLParser();
}
public String encodeRequest(...) throws ... HL7Exception
{
// prepare HL7 query request
QRY_A19 qryA19 = new QRY_A19();
...
return parser.encode(qryA19);
}
the encodeRequest() method works fine when called outside the tomcat environment (JUnit test) but fails when called from the Axis service:
I get the following warning while creating the class:
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
INFO - 2014-05-20 09:11:07,543 - Classe: ca.uhn.hl7v2.util.Home - Metodo: setHomeDirectory - Descrizione: hapi.home is set to C:\Programmi\eclipse-j2ee-helios\.
INFO - 2014-05-20 09:11:07,933 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: printHapiVersion - Descrizione: HAPI version is: 2.2
INFO - 2014-05-20 09:11:07,949 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkStructureLibraries - Descrizione: Default Structure libraries found for HL7 versions 2.5, 2.6,
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: Error occured while trying to retrieve a DOMImplementation.
java.lang.RuntimeException: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
at ca.uhn.hl7v2.util.XMLUtils.getDOMImpl(XMLUtils.java:55)
at ca.uhn.hl7v2.VersionLogger.checkDOMImplementation(VersionLogger.java:44)
at ca.uhn.hl7v2.VersionLogger.init(VersionLogger.java:36)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:126)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:112)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:103)
at ca.uhn.hl7v2.parser.Parser.<init>(Parser.java:71)
at ca.uhn.hl7v2.parser.XMLParser.<init>(XMLParser.java:89)
at ca.uhn.hl7v2.parser.DefaultXMLParser.<init>(DefaultXMLParser.java:81)
at com.avelco.integrazioni.areavasta.naar.Hl7MessageTranslator.<init>(Hl7MessageTranslator.java:105)
at com.avelco.integrazioni.areavasta.naar.NaarBridge.requestGrid(NaarBridge.java:135)
at com.avelco.integration.services.Service.requestGrid(Service.java:122)
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)
...
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: XML parsing and encoding as well as working with Conformance Profiles will fail.
and the following error while calling the parser.encode() method:
20/05/2014 09:11:08 : debug: Filling MSH ...
20/05/2014 09:11:08 : debug: Filling QRD ...
20/05/2014 09:11:08 : debug: Filling QRF ...
ca.uhn.hl7v2.HL7Exception: Can't create XML document - java.lang.RuntimeException
at ca.uhn.hl7v2.parser.DefaultXMLParser.encodeDocument(DefaultXMLParser.java:115)
at ca.uhn.hl7v2.parser.XMLParser.doEncode(XMLParser.java:241)
at ca.uhn.hl7v2.parser.Parser.encode(Parser.java:276)
at com.avelco.integrazioni.areavasta.naar.Hl7MessageTranslator.encodeRequest(Hl7MessageTranslator.java:174)
at com.avelco.integrazioni.areavasta.naar.NaarBridge.requestGrid(NaarBridge.java:144)
at com.avelco.integration.services.Service.requestGrid(Service.java:122)
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)
Since it works when called by JUnit it must be a dependency/configuration problem but I can't figure where to look for it.
Thanks in advance
Massimo
It is a jar conflict issue with axis dependencies. If you have any seperate log4j jar remove it.
At last I came out of this.
As Hussein Zawawi suggested it was a jar conflict with axis, only it was Xerces and not log4j the cause.
It seems that some HAPI DefaultXMLParser methods expected org.w3c.dom Objects but the factory in my environment produced org.apache.xerces.dom ones.
Unfortunately I couldn't remove Xerces from the project without breaking the legacy software I'm working with. Eventually I had to write my own implementation of DefaultXMLParser rewriting faulty methods and replacing XML parse and serializing calls.
public class AvXmlParser extends DefaultXMLParser
{
/*
* (non-Javadoc)
* #see ca.uhn.hl7v2.parser.DefaultXMLParser#encodeDocument(ca.uhn.hl7v2.model.Message)
*/
public Document encodeDocument(Message source) throws HL7Exception
{
String messageClassName = source.getClass().getName();
String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1);
org.w3c.dom.Document doc = null;
try
{
// doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc = XmlUtilities.emptyDom();
// Element root = doc.createElement(messageName);
Element root = doc.createElementNS("urn:hl7-org:v2xml",messageName);
doc.appendChild(root);
}
catch (Exception e)
{
throw new HL7Exception("Can't create XML document - " + e.getClass().getName(), e);
}
encode(source, doc.getDocumentElement());
return doc;
}
/**
* #param groupObject
* #param groupElement
* #throws HL7Exception
*/
private void encode(ca.uhn.hl7v2.model.Group groupObject, org.w3c.dom.Element groupElement) throws HL7Exception
{
String[] childNames = groupObject.getNames();
String messageName = groupObject.getMessage().getName();
try
{
for (int i = 0; i < childNames.length; i++)
{
Structure[] reps = groupObject.getAll(childNames[i]);
for (int j = 0; j < reps.length; j++)
{
Element childElement = groupElement.getOwnerDocument().createElement(makeGroupElementName(messageName, childNames[i]));
groupElement.appendChild(childElement);
if (reps[j] instanceof Group)
{
encode((Group) reps[j], childElement);
}
else if (reps[j] instanceof Segment)
{
encode((Segment) reps[j], childElement);
}
}
}
}
catch (DOMException e)
{
throw new HL7Exception("Can't encode group " + groupObject.getClass().getName(), e);
}
}
/*
* (non-Javadoc)
* #see ca.uhn.hl7v2.parser.XMLParser#parseStringIntoDocument(java.lang.String)
*/
protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception
{
try
{
Document doc = XmlUtilities.parseString(message);
return doc;
}
catch (Exception e)
{
throw new HL7Exception("Exception parsing XML",e);
}
}
}
(The XmlUtilities class is a utility class already available in my environment).
Most of the problems were so because of the legacy software I'm working with and not because of HAPI or Xerces but I hope my solution can be useful in future.

java.lang.NullPointerException in OpenNLP using RJB (Ruby Java Bridge)

I am trying to use the open-nlp Ruby gem to access the Java OpenNLP processor through RJB (Ruby Java Bridge). I am not a Java programmer, so I don't know how to solve this. Any recommendations regarding resolving it, debugging it, collecting more information, etc. would be appreciated.
The environment is Windows 8, Ruby 1.9.3p448, Rails 4.0.0, JDK 1.7.0-40 x586. Gems are rjb 1.4.8 and louismullie/open-nlp 0.1.4. For the record, this file runs in JRuby but I experience other problems in that environment and would prefer to stay native Ruby for now.
In brief, the open-nlp gem is failing with java.lang.NullPointerException and Ruby error method missing. I hesitate to say why this is happening because I don't know, but it appears to me that the dynamic loading of the Jars file opennlp.tools.postag.POSTaggerME#1b5080a cannot be accessed, perhaps because OpenNLP::Bindings::Utils.tagWithArrayList isn't being set up correctly. OpenNLP::Bindings is Ruby. Utils, and its methods, are Java. And Utils is supposedly the "default" Jars and Class files, which may be important.
What am I doing wrong, here? Thanks!
The code I am running is copied straight out of github/open-nlp. My copy of the code is:
class OpennlpTryer
$DEBUG=false
# From https://github.com/louismullie/open-nlp
# Hints: Dir.pwd; File.expand_path('../../Gemfile', __FILE__);
# Load the module
require 'open-nlp'
#require 'jruby-jars'
=begin
# Alias "write" to "print" to monkeypatch the NoMethod write error
java_import java.io.PrintStream
class PrintStream
java_alias(:write, :print, [java.lang.String])
end
=end
=begin
# Display path of jruby-jars jars...
puts JRubyJars.core_jar_path # => path to jruby-core-VERSION.jar
puts JRubyJars.stdlib_jar_path # => path to jruby-stdlib-VERSION.jar
=end
puts ENV['CLASSPATH']
# Set an alternative path to look for the JAR files.
# Default is gem's bin folder.
# OpenNLP.jar_path = '/path_to_jars/'
OpenNLP.jar_path = File.join(ENV["GEM_HOME"],"gems/open-nlp-0.1.4/bin/")
puts OpenNLP.jar_path
# Set an alternative path to look for the model files.
# Default is gem's bin folder.
# OpenNLP.model_path = '/path_to_models/'
OpenNLP.model_path = File.join(ENV["GEM_HOME"],"gems/open-nlp-0.1.4/bin/")
puts OpenNLP.model_path
# Pass some alternative arguments to the Java VM.
# Default is ['-Xms512M', '-Xmx1024M'].
# OpenNLP.jvm_args = ['-option1', '-option2']
OpenNLP.jvm_args = ['-Xms512M', '-Xmx1024M']
# Redirect VM output to log.txt
OpenNLP.log_file = 'log.txt'
# Set default models for a language.
# OpenNLP.use :language
OpenNLP.use :english # Make sure this is lower case!!!!
# Simple tokenizer
OpenNLP.load
sent = "The death of the poet was kept from his poems."
tokenizer = OpenNLP::SimpleTokenizer.new
tokens = tokenizer.tokenize(sent).to_a
# => %w[The death of the poet was kept from his poems .]
puts "Tokenize #{tokens}"
# Maximum entropy tokenizer, chunker and POS tagger
OpenNLP.load
chunker = OpenNLP::ChunkerME.new
tokenizer = OpenNLP::TokenizerME.new
tagger = OpenNLP::POSTaggerME.new
sent = "The death of the poet was kept from his poems."
tokens = tokenizer.tokenize(sent).to_a
# => %w[The death of the poet was kept from his poems .]
puts "Tokenize #{tokens}"
tags = tagger.tag(tokens).to_a
# => %w[DT NN IN DT NN VBD VBN IN PRP$ NNS .]
puts "Tags #{tags}"
chunks = chunker.chunk(tokens, tags).to_a
# => %w[B-NP I-NP B-PP B-NP I-NP B-VP I-VP B-PP B-NP I-NP O]
puts "Chunks #{chunks}"
# Abstract Bottom-Up Parser
OpenNLP.load
sent = "The death of the poet was kept from his poems."
parser = OpenNLP::Parser.new
parse = parser.parse(sent)
=begin
parse.get_text.should eql sent
parse.get_span.get_start.should eql 0
parse.get_span.get_end.should eql 46
parse.get_child_count.should eql 1
=end
child = parse.get_children[0]
child.text # => "The death of the poet was kept from his poems."
child.get_child_count # => 3
child.get_head_index #=> 5
child.get_type # => "S"
puts "Child: #{child}"
# Maximum Entropy Name Finder*
OpenNLP.load
# puts File.expand_path('.', __FILE__)
text = File.read('./spec/sample.txt').gsub!("\n", "")
tokenizer = OpenNLP::TokenizerME.new
segmenter = OpenNLP::SentenceDetectorME.new
puts "Tokenizer: #{tokenizer}"
puts "Segmenter: #{segmenter}"
ner_models = ['person', 'time', 'money']
ner_finders = ner_models.map do |model|
OpenNLP::NameFinderME.new("en-ner-#{model}.bin")
end
puts "NER Finders: #{ner_finders}"
sentences = segmenter.sent_detect(text)
puts "Sentences: #{sentences}"
named_entities = []
sentences.each do |sentence|
tokens = tokenizer.tokenize(sentence)
ner_models.each_with_index do |model, i|
finder = ner_finders[i]
name_spans = finder.find(tokens)
name_spans.each do |name_span|
start = name_span.get_start
stop = name_span.get_end-1
slice = tokens[start..stop].to_a
named_entities << [slice, model]
end
end
end
puts "Named Entities: #{named_entities}"
# Loading specific models
# Just pass the name of the model file to the constructor. The gem will search for the file in the OpenNLP.model_path folder.
OpenNLP.load
tokenizer = OpenNLP::TokenizerME.new('en-token.bin')
tagger = OpenNLP::POSTaggerME.new('en-pos-perceptron.bin')
name_finder = OpenNLP::NameFinderME.new('en-ner-person.bin')
# etc.
puts "Tokenizer: #{tokenizer}"
puts "Tagger: #{tagger}"
puts "Name Finder: #{name_finder}"
# Loading specific classes
# You may want to load specific classes from the OpenNLP library that are not loaded by default. The gem provides an API to do this:
# Default base class is opennlp.tools.
OpenNLP.load_class('SomeClassName')
# => OpenNLP::SomeClassName
# Here, we specify another base class.
OpenNLP.load_class('SomeOtherClass', 'opennlp.tools.namefind')
# => OpenNLP::SomeOtherClass
end
The line which is failing is line 73: (tokens == the sentence being processed.)
tags = tagger.tag(tokens).to_a #
# => %w[DT NN IN DT NN VBD VBN IN PRP$ NNS .]
tagger.tag calls open-nlp/classes.rb line 13, which is where the error is thrown. The code there is:
class OpenNLP::POSTaggerME < OpenNLP::Base
unless RUBY_PLATFORM =~ /java/
def tag(*args)
OpenNLP::Bindings::Utils.tagWithArrayList(#proxy_inst, args[0]) # <== Line 13
end
end
end
The Ruby error thrown at this point is: `method_missing': unknown exception (NullPointerException). Debugging this, I found the error java.lang.NullPointerException. args[0] is the sentence being processed. #proxy_inst is opennlp.tools.postag.POSTaggerME#1b5080a.
OpenNLP::Bindings sets up the Java environment. For example, it sets up the Jars to be loaded and the classes within those Jars. In line 54, it sets up defaults for RJB, which should set up OpenNLP::Bindings::Utils and its methods as follows:
# Add in Rjb workarounds.
unless RUBY_PLATFORM =~ /java/
self.default_jars << 'utils.jar'
self.default_classes << ['Utils', '']
end
utils.jar and Utils.java are in the CLASSPATH with the other Jars being loaded. They are being accessed, which is verified because the other Jars throw error messages if they are not present. The CLASSPATH is:
.;C:\Program Files (x86)Java\jdk1.7.0_40\lib;C:\Program Files (x86)Java\jre7\lib;D:\BitNami\rubystack-1.9.3-12\ruby\lib\ruby\gems\1.9.1\gems\open-nlp-0.1.4\bin
The applications Jars are in D:\BitNami\rubystack-1.9.3-12\ruby\lib\ruby\gems\1.9.1\gems\open-nlp-0.1.4\bin and, again, if they are not there I get error messages on other Jars. The Jars and Java files in ...\bin include:
jwnl-1.3.3.jar
opennlp-maxent-3.0.2-incubating.jar
opennlp-tools-1.5.2-incubating.jar
opennlp-uima-1.5.2-incubating.jar
utils.jar
Utils.java
Utils.java is as follows:
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.String;
import opennlp.tools.postag.POSTagger;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.namefind.NameFinderME; // interface instead?
import opennlp.tools.util.Span;
// javac -cp '.:opennlp.tools.jar' Utils.java
// jar cf utils.jar Utils.class
public class Utils {
public static String[] tagWithArrayList(POSTagger posTagger, ArrayList[] objectArray) {
return posTagger.tag(getStringArray(objectArray));
}
public static Object[] findWithArrayList(NameFinderME nameFinder, ArrayList[] tokens) {
return nameFinder.find(getStringArray(tokens));
}
public static Object[] chunkWithArrays(ChunkerME chunker, ArrayList[] tokens, ArrayList[] tags) {
return chunker.chunk(getStringArray(tokens), getStringArray(tags));
}
public static String[] getStringArray(ArrayList[] objectArray) {
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
return stringArray;
}
}
So, it should define tagWithArrayList and import opennlp.tools.postag.POSTagger. (OBTW, just to try, I changed the incidences of POSTagger to POSTaggerME in this file. It changed nothing...)
The tools Jar file, opennlp-tools-1.5.2-incubating.jar, includes postag/POSTagger and POSTaggerME class files, as expected.
Error messages are:
D:\BitNami\rubystack-1.9.3-12\ruby\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb
.;C:\Program Files (x86)\Java\jdk1.7.0_40\lib;C:\Program Files (x86)\Java\jre7\lib;D:\BitNami\rubystack-1.9.3-12\ruby\lib\ruby\gems\1.9.1\gems\open-nlp-0.1.4\bin
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/bin/
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/bin/
Tokenize ["The", "death", "of", "the", "poet", "was", "kept", "from", "his", "poems", "."]
Tokenize ["The", "death", "of", "the", "poet", "was", "kept", "from", "his", "poems", "."]
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:13:in `method_missing': unknown exception (NullPointerException)
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:13:in `tag'
from D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:73:in `<class:OpennlpTryer>'
from D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Modified Utils.java:
import java.util.Arrays;
import java.util.Object;
import java.lang.String;
import opennlp.tools.postag.POSTagger;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.namefind.NameFinderME; // interface instead?
import opennlp.tools.util.Span;
// javac -cp '.:opennlp.tools.jar' Utils.java
// jar cf utils.jar Utils.class
public class Utils {
public static String[] tagWithArrayList(POSTagger posTagger, Object[] objectArray) {
return posTagger.tag(getStringArray(objectArray));
}f
public static Object[] findWithArrayList(NameFinderME nameFinder, Object[] tokens) {
return nameFinder.find(getStringArray(tokens));
}
public static Object[] chunkWithArrays(ChunkerME chunker, Object[] tokens, Object[] tags) {
return chunker.chunk(getStringArray(tokens), getStringArray(tags));
}
public static String[] getStringArray(Object[] objectArray) {
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
return stringArray;
}
}
Modified error messages:
Uncaught exception: uninitialized constant OpennlpTryer::ArrayStoreException
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:81:in `rescue in <class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:77:in `<class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
Revised error with Utils.java revised to "import java.lang.Object;":
Uncaught exception: uninitialized constant OpennlpTryer::ArrayStoreException
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:81:in `rescue in <class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:77:in `<class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
Rescue removed from OpennlpTryer shows error trapped in classes.rb:
Uncaught exception: uninitialized constant OpenNLP::POSTaggerME::ArrayStoreException
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:16:in `rescue in tag'
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:13:in `tag'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:78:in `<class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
Same error but with all rescues removed so it's "native Ruby"
Uncaught exception: unknown exception
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:15:in `method_missing'
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp/classes.rb:15:in `tag'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:78:in `<class:OpennlpTryer>'
D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
Revised Utils.java:
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.String;
import opennlp.tools.postag.POSTagger;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.namefind.NameFinderME; // interface instead?
import opennlp.tools.util.Span;
// javac -cp '.:opennlp.tools.jar' Utils.java
// jar cf utils.jar Utils.class
public class Utils {
public static String[] tagWithArrayList(
System.out.println("Tokens: ("+objectArray.getClass().getSimpleName()+"): \n"+objectArray);
POSTagger posTagger, ArrayList[] objectArray) {
return posTagger.tag(getStringArray(objectArray));
}
public static Object[] findWithArrayList(NameFinderME nameFinder, ArrayList[] tokens) {
return nameFinder.find(getStringArray(tokens));
}
public static Object[] chunkWithArrays(ChunkerME chunker, ArrayList[] tokens, ArrayList[] tags) {
return chunker.chunk(getStringArray(tokens), getStringArray(tags));
}
public static String[] getStringArray(ArrayList[] objectArray) {
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
return stringArray;
}
}
I ran cavaj on Utils.class that I unzipped from util.jar and this is what I found. It differs from Utils.java by quite a bit. Both come installed with the open-nlp 1.4.8 gem. I don't know if this is the root cause of the problem, but this file is the core of where it breaks and we have a major discrepancy. Which should we use?
import java.util.ArrayList;
import java.util.Arrays;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.postag.POSTagger;
public class Utils
{
public Utils()
{
}
public static String[] tagWithArrayList(POSTagger postagger, ArrayList aarraylist[])
{
return postagger.tag(getStringArray(aarraylist));
}
public static Object[] findWithArrayList(NameFinderME namefinderme, ArrayList aarraylist[])
{
return namefinderme.find(getStringArray(aarraylist));
}
public static Object[] chunkWithArrays(ChunkerME chunkerme, ArrayList aarraylist[], ArrayList aarraylist1[])
{
return chunkerme.chunk(getStringArray(aarraylist), getStringArray(aarraylist1));
}
public static String[] getStringArray(ArrayList aarraylist[])
{
String as[] = (String[])Arrays.copyOf(aarraylist, aarraylist.length, [Ljava/lang/String;);
return as;
}
}
Utils.java in use as of 10/07, compiled and compressed into utils.jar:
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.String;
import opennlp.tools.postag.POSTagger;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.namefind.NameFinderME; // interface instead?
import opennlp.tools.util.Span;
// javac -cp '.:opennlp.tools.jar' Utils.java
// jar cf utils.jar Utils.class
public class Utils {
public static String[] tagWithArrayList(POSTagger posTagger, ArrayList[] objectArray) {
return posTagger.tag(getStringArray(objectArray));
}
public static Object[] findWithArrayList(NameFinderME nameFinder, ArrayList[] tokens) {
return nameFinder.find(getStringArray(tokens));
}
public static Object[] chunkWithArrays(ChunkerME chunker, ArrayList[] tokens, ArrayList[] tags) {
return chunker.chunk(getStringArray(tokens), getStringArray(tags));
}
public static String[] getStringArray(ArrayList[] objectArray) {
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
return stringArray;
}
}
Failures are occurring in BindIt::Binding::load_klass in line 110 here:
# Private function to load classes.
# Doesn't check if initialized.
def load_klass(klass, base, name=nil)
base += '.' unless base == ''
fqcn = "#{base}#{klass}"
name ||= klass
if RUBY_PLATFORM =~ /java/
rb_class = java_import(fqcn)
if name != klass
if rb_class.is_a?(Array)
rb_class = rb_class.first
end
const_set(name.intern, rb_class)
end
else
rb_class = Rjb::import(fqcn) # <== This is line 110
const_set(name.intern, rb_class)
end
end
The messages are as follows, however they are inconsistent in terms of the particular method that is identified. Each run may display a different method, any of POSTagger, ChunkerME, or NameFinderME.
D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:110:in `import': opennlp/tools/namefind/NameFinderME (NoClassDefFoundError)
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:110:in `load_klass'
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:89:in `block in load_default_classes'
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:87:in `each'
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:87:in `load_default_classes'
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/bind-it-0.2.7/lib/bind-it/binding.rb:56:in `bind'
from D:/BitNami/rubystack-1.9.3-12/ruby/lib/ruby/gems/1.9.1/gems/open-nlp-0.1.4/lib/open-nlp.rb:14:in `load'
from D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:54:in `<class:OpennlpTryer>'
from D:/BitNami/rubystack-1.9.3-12/projects/RjbTest/app/helpers/opennlp_tryer.rb:1:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
The interesting point about these errors are that they are originating in OpennlpTryer line 54 which is:
OpenNLP.load
At this point, OpenNLP fires up RJB which uses BindIt to load the jars and classes. This is well before the errors that I was seeing at the beginning of this question. However, I can't help but think it is all related. I really don't understand the inconsistency of these errors at all.
I was able to add the logging function in to Utils.java, compile it after adding in an "import java.io.*" and compress it. However, I pulled it out because of these errors as I didn't know if or not it was involved. I don't think it was. However, because these errors are occurring during load, the method is never called anyway so logging there won't help...
For each of the other jars, the jar is loaded then each class is imported using RJB. Utils is handled differently and is specified as the "default". From what I can tell, Utils.class is executed to load its own classes?
Later update on 10/07:
Here is where I am, I think. First, I have some problem replacing Utils.java, as I described earlier today. That problem probably needs solved before I can install a fix.
Second, I now understand the difference between POSTagger and POSTaggerME because the ME means Maximum Entropy. The test code is trying to call POSTaggerME but it looks to me like Utils.java, as implemented, supports POSTagger. I tried changing the test code to call POSTagger, but it said it couldn't find an initializer. Looking at the source for each of these, and I am guessing here, I think that POSTagger exists for the sole purpose to support POSTaggerME which implements it.
The source is opennlp-tools file opennlp-tools-1.5.2-incubating-sources.jar.
What I don't get is the whole reason for Utils in the first place? Why aren't the jars/classes provided in bindings.rb enough? This feels like a bad monkeypatch. I mean, look what bindings.rb does in the first place:
# Default JARs to load.
self.default_jars = [
'jwnl-1.3.3.jar',
'opennlp-tools-1.5.2-incubating.jar',
'opennlp-maxent-3.0.2-incubating.jar',
'opennlp-uima-1.5.2-incubating.jar'
]
# Default namespace.
self.default_namespace = 'opennlp.tools'
# Default classes.
self.default_classes = [
# OpenNLP classes.
['AbstractBottomUpParser', 'opennlp.tools.parser'],
['DocumentCategorizerME', 'opennlp.tools.doccat'],
['ChunkerME', 'opennlp.tools.chunker'],
['DictionaryDetokenizer', 'opennlp.tools.tokenize'],
['NameFinderME', 'opennlp.tools.namefind'],
['Parser', 'opennlp.tools.parser.chunking'],
['Parse', 'opennlp.tools.parser'],
['ParserFactory', 'opennlp.tools.parser'],
['POSTaggerME', 'opennlp.tools.postag'],
['SentenceDetectorME', 'opennlp.tools.sentdetect'],
['SimpleTokenizer', 'opennlp.tools.tokenize'],
['Span', 'opennlp.tools.util'],
['TokenizerME', 'opennlp.tools.tokenize'],
# Generic Java classes.
['FileInputStream', 'java.io'],
['String', 'java.lang'],
['ArrayList', 'java.util']
]
# Add in Rjb workarounds.
unless RUBY_PLATFORM =~ /java/
self.default_jars << 'utils.jar'
self.default_classes << ['Utils', '']
end
SEE FULL CODE AT END FOR THE COMPLETE CORRECTED CLASSES.RB MODULE
I ran into the same problem today. I didn't quite understand why the Utils class were being used, so I modified the classes.rb file in the following way:
unless RUBY_PLATFORM =~ /java/
def tag(*args)
#proxy_inst.tag(args[0])
#OpenNLP::Bindings::Utils.tagWithArrayList(#proxy_inst, args[0])
end
end
In that way I can make the following test to pass:
sent = "The death of the poet was kept from his poems."
tokens = tokenizer.tokenize(sent).to_a
# => %w[The death of the poet was kept from his poems .]
tags = tagger.tag(tokens).to_a
# => ["prop", "prp", "n", "v-fin", "n", "adj", "prop", "v-fin", "n", "adj", "punc"]
R_G Edit:
I tested that change and it eliminated the error. I am going to have to do more testing to ensure the outcome is what should be expected. However, following that same pattern, I made the following changes in classes.rb as well:
def chunk(tokens, tags)
chunks = #proxy_inst.chunk(tokens, tags)
# chunks = OpenNLP::Bindings::Utils.chunkWithArrays(#proxy_inst, tokens,tags)
chunks.map { |c| c.to_s }
end
...
class OpenNLP::NameFinderME < OpenNLP::Base
unless RUBY_PLATFORM =~ /java/
def find(*args)
#proxy_inst.find(args[0])
# OpenNLP::Bindings::Utils.findWithArrayList(#proxy_inst, args[0])
end
end
end
This allowed the entire sample test to execute without failure. I will provide a later update regarding verification of the results.
FINAL EDIT AND UPDATED CLASSES.RB per Space Pope and R_G:
As it turns out, this answer was key to the desired solution. However, the results were inconsistent as it was corrected. We continued to drill down into it and implemented strong typing during the calls, as specified by RJB. This converts the call to use of the _invoke method where the parameters include the desired method, the strong type, and the additional parameters. Andre's recommendation was key to the solution, so kudos to him. Here is the complete module. It eliminates the need for the Utils.class that was attempting to make these calls but failing. We plan to issue a github pull request for the open-nlp gem to update this module:
require 'open-nlp/base'
class OpenNLP::SentenceDetectorME < OpenNLP::Base; end
class OpenNLP::SimpleTokenizer < OpenNLP::Base; end
class OpenNLP::TokenizerME < OpenNLP::Base; end
class OpenNLP::POSTaggerME < OpenNLP::Base
unless RUBY_PLATFORM =~ /java/
def tag(*args)
#proxy_inst._invoke("tag", "[Ljava.lang.String;", args[0])
end
end
end
class OpenNLP::ChunkerME < OpenNLP::Base
if RUBY_PLATFORM =~ /java/
def chunk(tokens, tags)
if !tokens.is_a?(Array)
tokens = tokens.to_a
tags = tags.to_a
end
tokens = tokens.to_java(:String)
tags = tags.to_java(:String)
#proxy_inst.chunk(tokens,tags).to_a
end
else
def chunk(tokens, tags)
chunks = #proxy_inst._invoke("chunk", "[Ljava.lang.String;[Ljava.lang.String;", tokens, tags)
chunks.map { |c| c.to_s }
end
end
end
class OpenNLP::Parser < OpenNLP::Base
def parse(text)
tokenizer = OpenNLP::TokenizerME.new
full_span = OpenNLP::Bindings::Span.new(0, text.size)
parse_obj = OpenNLP::Bindings::Parse.new(
text, full_span, "INC", 1, 0)
tokens = tokenizer.tokenize_pos(text)
tokens.each_with_index do |tok,i|
start, stop = tok.get_start, tok.get_end
token = text[start..stop-1]
span = OpenNLP::Bindings::Span.new(start, stop)
parse = OpenNLP::Bindings::Parse.new(text, span, "TK", 0, i)
parse_obj.insert(parse)
end
#proxy_inst.parse(parse_obj)
end
end
class OpenNLP::NameFinderME < OpenNLP::Base
unless RUBY_PLATFORM =~ /java/
def find(*args)
#proxy_inst._invoke("find", "[Ljava.lang.String;", args[0])
end
end
end
I don't think you're doing anything wrong at all. You're also not the only one with this problem. It looks like a bug in Utils. Creating an ArrayList[] in Java doesn't make much sense - it's technically legal, but it would be an array of ArrayLists, which a) is just plain odd and b) terrible practice with regard to Java generics, and c) won't cast properly to String[] like the author intends in getStringArray().
Given the way the utility's written and the fact that OpenNLP does, in fact, expect to receive a String[] as input for its tag() method, my best guess is that the original author meant to have Object[] where they have ArrayList[] in the Utils class.
Update
To output to a file in the root of your project directory, try adjusting the logging like this (I added another line for printing the contents of the input array):
try {
File log = new File("log.txt");
FileWriter fileWriter = new FileWriter(log);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("Tokens ("+objectArray.getClass().getSimpleName()+"): \r\n"+objectArray.toString()+"\r\n");
bufferedWriter.write(Arrays.toString(objectArray));
bufferedWriter.close();
}
catch (Exception e) {
e.printStackTrace();
}

How to send attribute from Database to dJ to make Report

/*
* DynamicJasper: A library for creating reports dynamically by specifying
* columns, groups, styles, etc. at runtime. It also saves a lot of development
* time in many cases! (http://sourceforge.net/projects/dynamicjasper)
*
* Copyright (C) 2008 FDV Solutions (http://www.fdvsolutions.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
*
* License as published by the Free Software Foundation; either
*
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
*
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
*/
package ar.com.fdvs.dj.test;
import java.sql.*;
import java.awt.Color;
import java.util.Date;
import java.util.Locale;
import net.sf.jasperreports.view.*;
import ar.com.fdvs.dj.domain.AutoText;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.builders.StyleBuilder;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.core.DJConstants;
// import ar.com.fdvs.dj.test.*;
public class Main extends BaseDjReportTest {
public DynamicReport buildReport() throws Exception {
// Connection C = new Connection();
// C.Con();
CConnection C= new CConnection();
C.Connection();
Statement stmt;
ResultSet rs = null;
String SQL = "SELECT * FROM student";
stmt = C.Con().createStatement();
rs = stmt.executeQuery(SQL);
String res= "";
FastReportBuilder drb = new FastReportBuilder();
drb.setQuery(SQL, DJConstants.QUERY_LANGUAGE_SQL);
while (rs.next()){
res= rs.getString("Name");
**drb.addColumn("Name","Name", String.class.getName(),30);**
// drb.addc
}
//.addColumn("Branch", "branch", String.class.getName(),30)
// .addColumn("Item", "item", String.class.getName(),50)
// .addColumn("Item Code", "id", Long.class.getName(),30,true)
// .addColumn("Quantity", "quantity", Long.class.getName(),60,true)
// .addColumn("Amount", "amount", Float.class.getName(),70,true)
drb.addGroups(2);
DynamicReport sa =drb.build();
drb.setSubtitle("This report was generated at " + new Date())
.setTemplateFile("templates/TemplateReportTest.jrxml")
.setUseFullPageWidth(true);
Style atStyle = new StyleBuilder(true).setFont(Font.COMIC_SANS_SMALL).setTextColor(Color.red).build();
Style atStyle2 = new StyleBuilder(true).setFont(new Font(9, Font._FONT_TIMES_NEW_ROMAN, false, true, false)).setTextColor(Color.BLUE).build();
/***
* Adding many autotexts in the same position (header/footer and aligment) makes them to be one on top of the other
*/
//First add in the FOOTER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200,40, atStyle);
drb.addAutoText("Autotext below Page counter", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT);
//Note the styled text: <b>msimone</b>, valid tags are: <b>, <i> and <u>
drb.addAutoText("Created by <b>msimone</b>", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,200);
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,30,30,atStyle2);
drb.addAutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT,AutoText.PATTERN_DATE_DATE_TIME);
//Now in HEADER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,100,40);
drb.addAutoText("Autotext at top-left", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-left (2)", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-center", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_CENTER,200,atStyle);
// DynamicReport dr = drb.build();
//i18N, you can set a Locale, different tha n the default in the VM
drb.setReportLocale(new Locale("es","AR"));
drb.setReportLocale(new Locale("pt","BR"));
drb.setReportLocale(new Locale("fr","FR"));
return sa;
}
public static void main(String[] args) throws Exception {
**Main test = new Main();
test.testReport();**
JasperViewer.viewReport(test.jp);
JasperDesignViewer.viewReportDesign(test.jr);
//JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, test.getLayoutManager(),new HashMap()));
}
}
Writing this code i m getting following exception and i really cant figure out the reason
Exception in thread "main" net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : varchar at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1448)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:85)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at ar.com.fdvs.dj.test.BaseDjReportTest.testReport(BaseDjReportTest.java:93)
at ar.com.fdvs.dj.test.Main.main(Main.java:121)
Caused by: java.lang.NoSuchMethodException: Unknown property 'varchar' on class 'class ar.com.fdvs.dj.test.domain.Product'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 12 more
Java Result: 1
You may be aware of this already, but it appears that somewhere, the code is trying to access a property called "varchar" on the Product object:
Caused by: java.lang.NoSuchMethodException: Unknown property 'varchar' on class 'class ar.com.fdvs.dj.test.domain.Product' at .....
I don't see where this is happening in your example. It may be that there is a place where name is expected and you are instead passing in type. Or somewhere in your configuration, you've got name and type switched around, so it's looking for a field named "varchar". Does that make sense?
In general, I find that the "Caused by" portion of the error log is the most informative.

Categories

Resources