The Bytecode Club
Javassist problem - Printable Version

+- The Bytecode Club (https://the.bytecode.club)
+-- Forum: Lobby (https://the.bytecode.club/forumdisplay.php?fid=1)
+--- Forum: Programming (https://the.bytecode.club/forumdisplay.php?fid=86)
+--- Thread: Javassist problem (/showthread.php?tid=1218)



Javassist problem - roocoon - 05-24-2015

Hello.
I'm trying to replace a class method's code by some other code that requires more bytes.
I couldn't find any simple way to do that and investigation led me to Javassist that allows it and looks pretty simple to implement. (Or maybe not).

The system is Win 7 SP1 x64.
It has the latest x64 JDK 8 installed (u45) and Java works fine. I kept the x86 version off the system to avoid any conflicts for this exercise).
I copied the Javassist.jar into <java-home>/jre/lib/ext (one of their suggestions other than modifying the CLASSPATH).

Then I tried one of the Javassist samples (they all gave similar errors).
e.g.

javac sample/Test.java
(this one run without errors and created a Test.class in the \Sample folder)

java sample.Test
(this one gave the errors):

C:\Tools\Javassist>java sample.Test
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javassist/NotFoundExc
eption
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javassist.NotFoundException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more

----
Is it a matter of incompatibility between Javassist and the x64 version of JDK or have I missed something important?
Has anybody tried Javassist in a similar environment without problems?
Is there some other easier way to add code to some method at the bytecode level?

Thanks in advance for any pointers.


RE: Javassist problem - Bibl - 05-25-2015

I would recommend that you use ASM http://asm.ow2.org/ to manipulate classes.


RE: Javassist problem - roocoon - 05-25-2015

Thank you for the suggestion.
I read about and got it yesterday but so far it looks like a more complicated setup for me.
Keep in mind that I don't have the experience in the internals of Java that most of you have.

Certainly too much work to insert 3-4 bytes in a method (:

I'd still be curious why the Javassist sample doesn't work for me.
Would a 32-bit JDK behave differently? I couldn't find any mention which JDK version Javassist needs.


RE: Javassist problem - Bibl - 05-25-2015

Hmm you said that they recommend adding it into your java installation folder, I would add it to the buildpath and try to run it. Maybe use Eclipse to manage that more easily and see if it works.


RE: Javassist problem - roocoon - 05-25-2015

Thank you.
I just managed to run it. The problem was related to the CLASSPATH.
Even though the variable was pointing to the proper \lib and javassist.jar was in the \ext folder under it, for some reason it didn't pick it up.
It worked after I added javassist.jar in a -cp at the command line.

For reference, I got lots of useful info in a blog, starting here: h**pFacelineside/javarevisited.blogspot.gr/2011/06/noclassdeffounderror-exception-in.html

Fascinating subject this bytecode manipulation. And the BytecodeViewer is a joy to use. I'm glad I looked into it.

Much appreciated for all help.


RE: Javassist problem - Bibl - 05-26-2015

my pleasure