Java Javaアプリ → JNA ↓ JNI ↓ Native libffi → ユーザコード
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
long time;
char* tzone;
double longitude;
double latitude;
double altitude;
} GEODATA;
/**
* Get Location
* @param gd
*/
void getGeoData(GEODATA* gd) {
time_t now;
struct tm *t_st;
now = time(NULL);
t_st = localtime(&now);
gd->time = (long)now;
gd->tzone = t_st->tm_zone;
gd->longitude = 135.0;
gd->latitude = 35.0;
gd->altitude = 36000.0;
return;
}
package com.snail.jnaexam;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
import java.util.Date;
public class App {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("JNAExamC", CLibrary.class);
void getGeoData(GEODATA gd);
}
public static class GEODATA extends Structure {
public NativeLong time;
public String tzone;
public double longitude;
public double latitude;
public double altitude;
}
public static void main(String[] args) {
GEODATA gd = new GEODATA();
CLibrary.INSTANCE.getGeoData(gd);
System.out.println(new Date(gd.time.longValue()) + " (" + gd.tzone + ")");
System.out.println("(" + gd.longitude + "," + gd.latitude + ")" + gd.altitude);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.snail</groupId>
<artifactId>JNAExam</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>JNAExam</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
<exclude>javax.servlet:servlet-api</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies>
</project>
$ export LD_LIBRARY_PATH=/Users/Atsushi/NetBeans/JNAExamC/dist/Debug/GNU-MacOSX/ $ java -classpath /Users/Atsushi/NetBeans/JNAExam/target/JNAExam-1.0-SNAPSHOT.jar com.snail.jnaexam.App Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 Thu Jan 01 09:00:32 JST 1970 (JST) (135.0,35.0)36000.0
ちゃんと呼び出せているっぽい
なんか time(NULL) の返値が 0 になっているけど、JNAはちゃんと動いているから、まぁいいや。(Apple の gcc がおかしい?)
Linux だと
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
time_t now;
struct tm *t_st;
now = time(NULL);
t_st = localtime(&now);
printf("%d",(1900 + t_st->tm_year));
return 0;
}
は、2011 を返すんだけど・・・