Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling Libraries made from Java and gcj

843829Jun 10 2008 — edited Jun 16 2008
Hello all,

I have a rather unique (to my knowledge) problem that I'd like some help with.
I'm in the process of updating some code from a few years ago and one of the things that I'm to be doing with it is to make it so that the source cannot be decompiled (or at least, much harder to decompile). There's two ways to do this: Obfuscation and Native Compilation, and for the sake of increased speed and efficiency (for the code and not myself) it was decided that it should be done via native compilation of Java.

Basically what I'm looking at is taking a lot of external java classes and turning them into shared libraries that are loaded into a main that is run on the JVM. Now, I can rewrite them in C and use JNI, which, does work, but I was wondering if there was an easier way. Maybe to take Java, use gcj (with CNI or JNI) to create a library with the files we already have, and then load it into the java bytecode.

After a lot of googling and toying around with this, I've been able to create a Main that does load a library that I've created using java, but it just hangs up when I run it. I've tried forcing the path to the library with System.load() to fix the error, but it doesn't help.
Even stranger is when I take the same code and then compile it under linux. I get an UnsatisfiedLinkError even though I forced the path.

This is all proof of concept stuff at the moment (just to see if I can get this working) and I was wondering if there is a way to make this work?


The Main:
import java.lang.UnsatisfiedLinkError;
import java.lang.SecurityException;

public class HelloMain
{

	public static native void write();

	public static void main(String[] args)
	{
	
		try
		{
			System.loadLibrary("HelloWorld");
		} catch( UnsatisfiedLinkError e ) {
			System.out.println("Link Error: " + e);
		} catch ( SecurityException e ) {
			System.out.println("Security Exception Caught: " + e);
		}
		
                write();	
	}
}
The External Library:
public class HelloWorld
{
	public static void write()
	{
		System.out.println("Hello World!");
	}
}
The commands:
+$gcj -C HelloMain.java+
+$gcj -fjni -c -g -O HelloWorld.java+
+$gcj -shared -o libHelloWorld.so HelloWorld.o+
+$java HelloMain+

Any help would be most appreciated.
-Rodgers
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2008
Added on Jun 10 2008
10 comments
105 views