• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaCompiler wont work with HashMap  RSS feed

 
boizy daze
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This may have a simple soulution (I hope so). I can compile dynamic java source code with the JavaCompiler, as long as I only use Integers and Strings. If I try passing a HashMap to or from my compiled method, I get errors.

The compile seems to work, it generates a .class file. But when I try to invoke the class, I get errors.


Here's the source code I'm trying to compile:



Here's my comiler code (nearly unchanged from a working example I found on line):




Here's my reflections stuff I use to load and invoke the new class:



When I look at the returnType and the parameterTypes, they are as they should be (String and HashMap respectively).

I must be using the method.invoke() call wrong?


And my error:

 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, my first question should really be "Is this different from what happens when you compile that ResultsProcessor class in the normal way?" That would be the first thing to ask yourself if you're going to point the finger at JavaCompiler. However I notice that you're trying to call the "sayHello" method as if it were a static method; that's what the first parameter of method.invoke is for. Instead, pass a ResultsProcessor object to method.invoke -- or declare the sayHello method to be static.
 
boizy daze
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you thank you! You made my day.

I knew I was confused about the static/non-static choice, and I new I was confused by what to send in the first parameter. You cleared it up with a single slap to my head.

Then my next confusion immediately followed... I wasn't sure how to pass a ResultsProcessor since it doesn't exist until it's compiled. Then my brain sputtered out a small flicker of intelligence and it came together:



Not sure if that's the best implementation... but it got me back on track.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!