Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  jnigen: JNI with less pain  (Read 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Posted 2014-01-02 19:24:17 »

I think jnigen is quite neat and I finally got around to writing some better documentation:

https://github.com/libgdx/libgdx/wiki/jnigen

libgdx uses it to wrap Box2D, libjpeg, vorbis, mpeg123, and many other things, but jnigen isn't specific to libgdx. It can be used to build any native libraries for use with Java. Eg, Jglfw uses it to wrap GLFW. Next time you need to interface with native code, maybe you keep it in mind! Smiley

Offline gouessej
« Reply #1 - Posted 2014-01-03 10:22:59 »

Hi

It's not that bad  Tongue You can directly write the C/C++ source code in the Java source code, it supports iOS too and it seems documented but GlueGen already does a very good job on OpenGL, OpenAL and OpenCL. If a developer wants to keep C/C++ and Java source code into separate files, GlueGen should be used, otherwise jnigen is a better choice.

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2014-01-03 10:50:15 »

I'm not familiar with GlueGen. Gave it a very quick look. It looks limited to C, so if you need C++ that might be an issue. I can't comment much further on it without digging deeper, though I can say mapping C to Java looks quite complex.

It seems the biggest difference between jnigen and tools that generate Java code from native code (GlueGen, SWIG, etc) is that jnigen allows you to write and control the Java code. Typically this will mean a nicer Java API, since it is handwritten and customized for the intended usage. I do concede that it can reduce development time to generate Java code from native code.

jnigen also feels to me like a simpler approach to the problem. You end up with exactly what you'd have if you were hand writing JNI: Java native methods and corresponding native code (in a separate file). jnigen reduces the pain of JNI by generating and populating the native code and handling the build for all platforms. If you rename a Java native method, add new native methods, new classes, change method signatures, etc you don't have to do anything.

jnigen should work great for projects that want to keep source in separate files. If I were doing that, I would still use native code inline with the Java code, but only for the Java to native interface. The rest of the code I would put in a separate file. jnigen has a special "/*JNI" comment for includes, statics, functions, etc. The contents just get inserted in your native code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Example {
   /*JNI
   #include <otherStuff.h>
   #include <moreStuff.h>
   */


   static public native void doStuff (); /*
      // use included stuff
   */

}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2014-01-03 21:02:16 »

Quote
run:
C/C++ for 'src/pl/mac70/jdirect/Window.java' up to date
Wrote target 'Windows' build script 'jni\build-windows32.xml'
Wrote master build script 'jni\build.xml'
Executing 'ant.bat -f D:\jDirect\jDirect\jni\build-windows32.xml -v -Dhas-compiler=true clean postcompile'
Executing 'ant.bat -f D:\jDirect\jDirect\jni\build.xml -v pack-natives'
java.io.IOException: Cannot run program "ant.bat": CreateProcess error=2, Nie mo¿na odnaleŸæ okreœlonego pl
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
   at com.badlogic.gdx.jnigen.BuildExecutor.startProcess(BuildExecutor.java:51)
   at com.badlogic.gdx.jnigen.BuildExecutor.executeAnt(BuildExecutor.java:38)
   at pl.mac70.test.JNIBuild.main(JNIBuild.java:17)
Caused by: java.io.IOException: CreateProcess error=2, Nie mo¿na odnaleŸæ okreœlonego pl
   at java.lang.ProcessImpl.create(Native Method)
   at java.lang.ProcessImpl.<init>(ProcessImpl.java:385)
   at java.lang.ProcessImpl.start(ProcessImpl.java:136)
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
   ... 3 more
java.io.IOException: Cannot run program "ant.bat": CreateProcess error=2, Nie mo¿na odnaleŸæ okreœlonego pl
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
   at com.badlogic.gdx.jnigen.BuildExecutor.startProcess(BuildExecutor.java:51)
   at com.badlogic.gdx.jnigen.BuildExecutor.executeAnt(BuildExecutor.java:38)
   at pl.mac70.test.JNIBuild.main(JNIBuild.java:19)
Caused by: java.io.IOException: CreateProcess error=2, Nie mo¿na odnaleŸæ okreœlonego pl
   at java.lang.ProcessImpl.create(Native Method)
   at java.lang.ProcessImpl.<init>(ProcessImpl.java:385)
   at java.lang.ProcessImpl.start(ProcessImpl.java:136)
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
   ... 3 more
BUILD SUCCESSFUL (total time: 0 seconds)

Any ideas? Roll Eyes Error(s) caused by this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
package pl.mac70.test;

import com.badlogic.gdx.jnigen.*;
import com.badlogic.gdx.jnigen.BuildTarget.TargetOs;

public class JNIBuild {

    static public void main (String[] args) throws Exception {
        NativeCodeGenerator jnigen = new NativeCodeGenerator();
        jnigen.generate("src", "build/classes", "jni", new String[] {"**/*"}, null);

        BuildTarget win32 = BuildTarget.newDefaultTarget(TargetOs.Windows, false);
        win32.compilerPrefix = "mingw32-";
        //BuildTarget win64 = BuildTarget.newDefaultTarget(TargetOs.Windows, true);

        new AntScriptGenerator().generate(new BuildConfig("jDirectDll"), win32);
        BuildExecutor.executeAnt("jni/build-windows32.xml", "-v -Dhas-compiler=true clean postcompile");
        //BuildExecutor.executeAnt("jni/build-windows64.xml", "-v -Dhas-compiler=true clean postcompile");
       BuildExecutor.executeAnt("jni/build.xml", "-v pack-natives");
    }
   
}

Check out my Devblog! Smiley
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2014-01-03 21:18:36 »

Ant (and MinGW) needs to be on your path.

Offline Mac70
« Reply #5 - Posted 2014-01-03 21:30:23 »

Both of them are added, still the same error. Path is working correctly via cmd for both mingw32-C++ (and everything else with "mingw32-" in name) and ant.bat

Check out my Devblog! Smiley
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2014-01-03 21:37:36 »

Restart Eclipse, probably it doesn't see your path changes.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (28 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!