4

I was fooling around with JNA trying to execute some C code in a Java program. This is a working example I found online (JNA required in build path):

package core;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class CoreController {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
                (Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);

        void printf(String format, Object... args);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i = 0; i < args.length; i++) {
            CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }

        Native.main(args);
    }
}

Actually, I am trying to do three (seemingly rediculess) things.

1.) The entry point of the program should be changed to the following C signature:

void __stdcall RVExtension(char *output, int outputSize, const char *function);

2.) The Java program should be able set the given output parameter.
3.) The program should be compiled to a DLL.

In C++, this issue would be resolved like this:

#include "stdafx.h"

extern "C" {
    __declspec (dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
}

void __stdcall RVExtension(char *output, int outputSize, const char *function) {
    strncpy_s(output, outputSize, "IT WORKS!", _TRUNCATE);
}

So the question is, is that somehow possible with Java? If so, I'd be glad to see some code example as I am entery a lot of new territory here. I don't even know if JNA is a proper solution here. If anyone has another idea, please tell!

Kind regards,
jaySon

jaySon
  • 795
  • 2
  • 7
  • 20
  • Well, I'll try to clearify: I'd like to change the entry point of a Java program to the above given C signature and would like to create a DLL file out of that Java program. Probably you're right about making the cpp program the entry point, but that's a totally different approach and I'd need help there as well. – jaySon Dec 29 '14 at 00:39
  • 1
    AFAIK can't natively compile Java to a DLL anyway. So I have no idea why you think your current approach will work, but like the maid; I don't do Windows. Good luck! – Elliott Frisch Dec 29 '14 at 00:44
  • @ElliottFrisch, well I've read [this](http://stackoverflow.com/questions/262603/is-there-any-way-to-compile-java-code-into-a-dll), and though I haven't tried it yet, I am trusting in the possibility. – jaySon Dec 29 '14 at 00:46
  • That tool is research grade, and it compiles to a .NET DLL. – Elliott Frisch Dec 29 '14 at 00:48
  • Not gonna work, unless you have some sort of super-special Java setup. Java doesn't "compile" to a conventional program. – Hot Licks Dec 29 '14 at 00:49
  • @HotLicks, I know that. That's why I linked this IKVM question and that's also why I made this whole question so vague as I am very doubtful myself if that's possible at all. But I was/am hoping for it to be possible as my personally preferred programming language is Java. – jaySon Dec 29 '14 at 00:53
  • Well, it makes no sense to say "the entry point of the program should be changed..." since a Java program has no entry point. And even if it did, Java has no idea what a `char*` parm is. – Hot Licks Dec 29 '14 at 01:00
  • I don't understand why a `public static void main(String[] args)` is not an entry point, but as for the `char*`, that's why I was playing around with JNA, hoping to find a solution to solve that issue. – jaySon Dec 29 '14 at 01:06
  • There is absolutely nothing special about a `main` method in a class, other than its name. – Hot Licks Dec 29 '14 at 04:15

1 Answers1

1

You'd have to write a regular C DLL and use the Java Invocation API to create a Java VM inside the process and call your Java program from there. That way you can use any entry point you want. JNA doesn't help here.

user2543253
  • 2,143
  • 19
  • 20