diff --git a/.gitignore b/.gitignore index b8bd026..67e4056 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,18 @@ *.exe *.out *.app + +#emacs backup file +*~ + +#crash log +hs_err*.log + + +/CppWrapJavaExampleDynamicLibrary/build/ +/CppCallJavaApplication/build/ +/CppCallJavaApplication/dist/ +/CppCallJavaApplication/nbproject/private/ +/CppWrapJavaExampleDynamicLibrary/dist/ +/CppWrapJavaExampleDynamicLibrary/nbproject/private + diff --git a/CppCallJavaApplication/.dep.inc b/CppCallJavaApplication/.dep.inc new file mode 100644 index 0000000..4560e55 --- /dev/null +++ b/CppCallJavaApplication/.dep.inc @@ -0,0 +1,5 @@ +# This code depends on make tool being used +DEPFILES=$(wildcard $(addsuffix .d, ${OBJECTFILES})) +ifneq (${DEPFILES},) +include ${DEPFILES} +endif diff --git a/CppCallJavaApplication/Main.class b/CppCallJavaApplication/Main.class new file mode 100644 index 0000000..350f5df Binary files /dev/null and b/CppCallJavaApplication/Main.class differ diff --git a/CppCallJavaApplication/Main.java b/CppCallJavaApplication/Main.java new file mode 100644 index 0000000..9a1a702 --- /dev/null +++ b/CppCallJavaApplication/Main.java @@ -0,0 +1,21 @@ + + + +public class Main { + + private final int i; + + public Main(int i) { + this.i = i; + System.out.println("java constructor for Main("+i+") called."); + } + + public static void staticTest(int i) { + System.out.println("java Main.staticTest("+i+") called."); + } + + public int getI() { + System.out.println("java Main.getI() returning:"+i); + return i; + } +} diff --git a/CppCallJavaApplication/Makefile b/CppCallJavaApplication/Makefile new file mode 100644 index 0000000..469a2e2 --- /dev/null +++ b/CppCallJavaApplication/Makefile @@ -0,0 +1,137 @@ +# +# There exist several targets which are by default empty and which can be +# used for execution of your targets. These targets are usually executed +# before and after some main targets. They are: +# +# .build-pre: called before 'build' target +# .build-post: called after 'build' target +# .clean-pre: called before 'clean' target +# .clean-post: called after 'clean' target +# .clobber-pre: called before 'clobber' target +# .clobber-post: called after 'clobber' target +# .all-pre: called before 'all' target +# .all-post: called after 'all' target +# .help-pre: called before 'help' target +# .help-post: called after 'help' target +# +# Targets beginning with '.' are not intended to be called on their own. +# +# Main targets can be executed directly, and they are: +# +# build build a specific configuration +# clean remove built files from a configuration +# clobber remove all built files +# all build all configurations +# help print help mesage +# +# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and +# .help-impl are implemented in nbproject/makefile-impl.mk. +# +# Available make variables: +# +# CND_BASEDIR base directory for relative paths +# CND_DISTDIR default top distribution directory (build artifacts) +# CND_BUILDDIR default top build directory (object files, ...) +# CONF name of current configuration +# CND_PLATFORM_${CONF} platform name (current configuration) +# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) +# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) +# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) +# CND_PACKAGE_DIR_${CONF} directory of package (current configuration) +# CND_PACKAGE_NAME_${CONF} name of package (current configuration) +# CND_PACKAGE_PATH_${CONF} path to package (current configuration) +# +# NOCDDL + + +# Environment +MKDIR=mkdir +CP=cp +CCADMIN=CCadmin +JDK_DIR?=/usr/lib/jvm/java-7-openjdk-amd64/ +JRE_LIB_DIR?=$(JDK_DIR)/jre/lib/amd64/server/ +JAVAC?=$(JDK_DIR)/bin/javac + + +# build +build: .build-post + +.build-pre: +# Add your pre 'build' code here... + +.build-post: .build-impl + $(JAVAC) *.java + + +# clean +clean: .clean-post + +.clean-pre: +# Add your pre 'clean' code here... + +.clean-post: .clean-impl + -rm *.class || true + + +# clobber +clobber: .clobber-post + +.clobber-pre: +# Add your pre 'clobber' code here... + +.clobber-post: .clobber-impl +# Add your post 'clobber' code here... + + +# all +all: .all-post + +.all-pre: +# Add your pre 'all' code here... + +.all-post: .all-impl +# Add your post 'all' code here... + + +# build tests +build-tests: .build-tests-post + +.build-tests-pre: +# Add your pre 'build-tests' code here... + +.build-tests-post: .build-tests-impl +# Add your post 'build-tests' code here... + + +# run tests +test: .test-post + +.test-pre: build-tests +# Add your pre 'test' code here... + +.test-post: .test-impl +# Add your post 'test' code here... + + +# help +help: .help-post + +.help-pre: +# Add your pre 'help' code here... + +.help-post: .help-impl +# Add your post 'help' code here... + + + +# include project implementation makefile +include nbproject/Makefile-impl.mk + +# include project make variables +include nbproject/Makefile-variables.mk + +testRun: build + -pwd + ${CND_ARTIFACT_PATH_${CONF}} + +.PHONY: testRun \ No newline at end of file diff --git a/CppCallJavaApplication/main.cpp b/CppCallJavaApplication/main.cpp new file mode 100644 index 0000000..456d09b --- /dev/null +++ b/CppCallJavaApplication/main.cpp @@ -0,0 +1,35 @@ +/* + * File: main.cpp + * Author: Will Shackleford {@literal } + * + * Created on July 30, 2015, 2:25 PM + */ + +#include +#include +#include +#include +#include + +using namespace std; +using namespace javamain; + +/* + * + */ +int main(int argc, char** argv) { + Main::staticTest(200); + Main::staticTest(300); + const jint origI = (jint) 400; + Main *m = new Main(origI); + jint i = m->getI(); + std::cerr << "cpp m->getI() returned: " << i << std::endl; + delete m; + closeJVM(); + if(i!= origI) { + cerr << i<<"!=" < Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf + + +# clean +.clean-impl: .clean-pre .validate-impl .depcheck-impl + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf + + +# clobber +.clobber-impl: .clobber-pre .depcheck-impl + @#echo "=> Running $@..." + for CONF in ${ALLCONFS}; \ + do \ + "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ + done + +# all +.all-impl: .all-pre .depcheck-impl + @#echo "=> Running $@..." + for CONF in ${ALLCONFS}; \ + do \ + "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ + done + +# build tests +.build-tests-impl: .build-impl .build-tests-pre + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf + +# run tests +.test-impl: .build-tests-impl .test-pre + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf + +# dependency checking support +.depcheck-impl: + @echo "# This code depends on make tool being used" >.dep.inc + @if [ -n "${MAKE_VERSION}" ]; then \ + echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \ + echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ + echo "include \$${DEPFILES}" >>.dep.inc; \ + echo "endif" >>.dep.inc; \ + else \ + echo ".KEEP_STATE:" >>.dep.inc; \ + echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ + fi + +# configuration validation +.validate-impl: + @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ + then \ + echo ""; \ + echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ + echo "See 'make help' for details."; \ + echo "Current directory: " `pwd`; \ + echo ""; \ + fi + @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ + then \ + exit 1; \ + fi + + +# help +.help-impl: .help-pre + @echo "This makefile supports the following configurations:" + @echo " ${ALLCONFS}" + @echo "" + @echo "and the following targets:" + @echo " build (default target)" + @echo " clean" + @echo " clobber" + @echo " all" + @echo " help" + @echo "" + @echo "Makefile Usage:" + @echo " make [CONF=] [SUB=no] build" + @echo " make [CONF=] [SUB=no] clean" + @echo " make [SUB=no] clobber" + @echo " make [SUB=no] all" + @echo " make help" + @echo "" + @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," + @echo " also build subprojects." + @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," + @echo " also clean subprojects." + @echo "Target 'clobber' will remove all built files from all configurations and," + @echo " unless 'SUB=no', also from subprojects." + @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," + @echo " also build subprojects." + @echo "Target 'help' prints this message." + @echo "" + diff --git a/CppCallJavaApplication/nbproject/Makefile-variables.mk b/CppCallJavaApplication/nbproject/Makefile-variables.mk new file mode 100644 index 0000000..c14cf25 --- /dev/null +++ b/CppCallJavaApplication/nbproject/Makefile-variables.mk @@ -0,0 +1,35 @@ +# +# Generated - do not edit! +# +# NOCDDL +# +CND_BASEDIR=`pwd` +CND_BUILDDIR=build +CND_DISTDIR=dist +# Debug configuration +CND_PLATFORM_Debug=GNU-Linux-x86 +CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-Linux-x86 +CND_ARTIFACT_NAME_Debug=cppcalljavaapplication +CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-Linux-x86/cppcalljavaapplication +CND_PACKAGE_DIR_Debug=dist/Debug/GNU-Linux-x86/package +CND_PACKAGE_NAME_Debug=cppcalljavaapplication.tar +CND_PACKAGE_PATH_Debug=dist/Debug/GNU-Linux-x86/package/cppcalljavaapplication.tar +# Release configuration +CND_PLATFORM_Release=GNU-Linux-x86 +CND_ARTIFACT_DIR_Release=dist/Release/GNU-Linux-x86 +CND_ARTIFACT_NAME_Release=cppcalljavaapplication +CND_ARTIFACT_PATH_Release=dist/Release/GNU-Linux-x86/cppcalljavaapplication +CND_PACKAGE_DIR_Release=dist/Release/GNU-Linux-x86/package +CND_PACKAGE_NAME_Release=cppcalljavaapplication.tar +CND_PACKAGE_PATH_Release=dist/Release/GNU-Linux-x86/package/cppcalljavaapplication.tar +# +# include compiler specific variables +# +# dmake command +ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ + (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) +# +# gmake command +.PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) +# +include nbproject/private/Makefile-variables.mk diff --git a/CppCallJavaApplication/nbproject/Package-Debug.bash b/CppCallJavaApplication/nbproject/Package-Debug.bash new file mode 100644 index 0000000..ded1d70 --- /dev/null +++ b/CppCallJavaApplication/nbproject/Package-Debug.bash @@ -0,0 +1,76 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=GNU-Linux-x86 +CND_CONF=Debug +CND_DISTDIR=dist +CND_BUILDDIR=build +CND_DLIB_EXT=so +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/cppcalljavaapplication +OUTPUT_BASENAME=cppcalljavaapplication +PACKAGE_TOP_DIR=cppcalljavaapplication/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/cppcalljavaapplication/bin" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cppcalljavaapplication.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cppcalljavaapplication.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff --git a/CppCallJavaApplication/nbproject/Package-Release.bash b/CppCallJavaApplication/nbproject/Package-Release.bash new file mode 100644 index 0000000..76acc49 --- /dev/null +++ b/CppCallJavaApplication/nbproject/Package-Release.bash @@ -0,0 +1,76 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=GNU-Linux-x86 +CND_CONF=Release +CND_DISTDIR=dist +CND_BUILDDIR=build +CND_DLIB_EXT=so +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/cppcalljavaapplication +OUTPUT_BASENAME=cppcalljavaapplication +PACKAGE_TOP_DIR=cppcalljavaapplication/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/cppcalljavaapplication/bin" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cppcalljavaapplication.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cppcalljavaapplication.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff --git a/CppCallJavaApplication/nbproject/configurations.xml b/CppCallJavaApplication/nbproject/configurations.xml new file mode 100644 index 0000000..54b3521 --- /dev/null +++ b/CppCallJavaApplication/nbproject/configurations.xml @@ -0,0 +1,136 @@ + + + + + + + + + main.cpp + + + + + Makefile + + + Makefile + + + + default + true + false + + + + + ../CppWrapJavaExampleDynamicLibrary + ${JDK_DIR}/include + ${JDK_DIR}/include/linux + /usr/lib/jvm/java-7-openjdk-amd64/include/linux + /usr/lib/jvm/java-7-openjdk-amd64/include + + + + + ${JRE_LIB_DIR} + + + + + + + jvm + -Wl,-rpath,${JRE_LIB_DIR} + + + + + + + + + + + + + default + true + false + + + + 5 + + + 5 + + ../CppWrapJavaExampleDynamicLibrary + ${JDK_DIR}/include + ${JDK_DIR}/include/linux + /usr/lib/jvm/java-7-openjdk-amd64/include/linux + /usr/lib/jvm/java-7-openjdk-amd64/include + + + + 5 + + + 5 + + + + ${JRE_LIB_DIR} + + + + + + + jvm + -Wl,-rpath,${JRE_LIB_DIR} + + + + + + + + diff --git a/CppCallJavaApplication/nbproject/project.xml b/CppCallJavaApplication/nbproject/project.xml new file mode 100644 index 0000000..0764408 --- /dev/null +++ b/CppCallJavaApplication/nbproject/project.xml @@ -0,0 +1,30 @@ + + + org.netbeans.modules.cnd.makeproject + + + CppCallJavaApplication + + cpp + + UTF-8 + + ../CppWrapJavaExampleDynamicLibrary + + + + + Debug + 1 + + + Release + 1 + + + + false + + + + diff --git a/CppWrapJavaExampleDynamicLibrary/.dep.inc b/CppWrapJavaExampleDynamicLibrary/.dep.inc new file mode 100644 index 0000000..4560e55 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/.dep.inc @@ -0,0 +1,5 @@ +# This code depends on make tool being used +DEPFILES=$(wildcard $(addsuffix .d, ${OBJECTFILES})) +ifneq (${DEPFILES},) +include ${DEPFILES} +endif diff --git a/CppWrapJavaExampleDynamicLibrary/Makefile b/CppWrapJavaExampleDynamicLibrary/Makefile new file mode 100644 index 0000000..e59c5ea --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/Makefile @@ -0,0 +1,130 @@ +# +# There exist several targets which are by default empty and which can be +# used for execution of your targets. These targets are usually executed +# before and after some main targets. They are: +# +# .build-pre: called before 'build' target +# .build-post: called after 'build' target +# .clean-pre: called before 'clean' target +# .clean-post: called after 'clean' target +# .clobber-pre: called before 'clobber' target +# .clobber-post: called after 'clobber' target +# .all-pre: called before 'all' target +# .all-post: called after 'all' target +# .help-pre: called before 'help' target +# .help-post: called after 'help' target +# +# Targets beginning with '.' are not intended to be called on their own. +# +# Main targets can be executed directly, and they are: +# +# build build a specific configuration +# clean remove built files from a configuration +# clobber remove all built files +# all build all configurations +# help print help mesage +# +# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and +# .help-impl are implemented in nbproject/makefile-impl.mk. +# +# Available make variables: +# +# CND_BASEDIR base directory for relative paths +# CND_DISTDIR default top distribution directory (build artifacts) +# CND_BUILDDIR default top build directory (object files, ...) +# CONF name of current configuration +# CND_PLATFORM_${CONF} platform name (current configuration) +# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) +# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) +# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) +# CND_PACKAGE_DIR_${CONF} directory of package (current configuration) +# CND_PACKAGE_NAME_${CONF} name of package (current configuration) +# CND_PACKAGE_PATH_${CONF} path to package (current configuration) +# +# NOCDDL + + +# Environment +MKDIR=mkdir +CP=cp +CCADMIN=CCadmin +JDK_DIR?=/usr/lib/jvm/java-7-openjdk-amd64/ +JRE_LIB_DIR?=$(JDK_DIR)/jre/lib/amd64/server/ +JAVAC?=$(JDK_DIR)/bin/javac + +# build +build: .build-post + +.build-pre: +# Add your pre 'build' code here... + +.build-post: .build-impl +# Add your post 'build' code here... + + +# clean +clean: .clean-post + +.clean-pre: +# Add your pre 'clean' code here... + +.clean-post: .clean-impl +# Add your post 'clean' code here... + + +# clobber +clobber: .clobber-post + +.clobber-pre: +# Add your pre 'clobber' code here... + +.clobber-post: .clobber-impl +# Add your post 'clobber' code here... + + +# all +all: .all-post + +.all-pre: +# Add your pre 'all' code here... + +.all-post: .all-impl +# Add your post 'all' code here... + + +# build tests +build-tests: .build-tests-post + +.build-tests-pre: +# Add your pre 'build-tests' code here... + +.build-tests-post: .build-tests-impl +# Add your post 'build-tests' code here... + + +# run tests +test: .test-post + +.test-pre: build-tests +# Add your pre 'test' code here... + +.test-post: .test-impl +# Add your post 'test' code here... + + +# help +help: .help-post + +.help-pre: +# Add your pre 'help' code here... + +.help-post: .help-impl +# Add your post 'help' code here... + + + +# include project implementation makefile +include nbproject/Makefile-impl.mk + +# include project make variables +include nbproject/Makefile-variables.mk diff --git a/CppWrapJavaExampleDynamicLibrary/javamain.cpp b/CppWrapJavaExampleDynamicLibrary/javamain.cpp new file mode 100644 index 0000000..97d357b --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/javamain.cpp @@ -0,0 +1,142 @@ +/* + * File: main.cpp + * Author: Will Shackleford {@literal } + * + * Created on July 30, 2015, 10:03 AM + */ + +#include +#include +#include "javamain.h" +#include + +using namespace std; +using namespace javamain; + + +static JNIEnv *getNewEnv() { + JavaVM *jvm; /* denotes a Java VM */ + JNIEnv *env; /* pointer to native method interface */ + JavaVM * jvmBuf[1]; + jsize nVMs; + jint v = JNI_GetCreatedJavaVMs(jvmBuf, 1, &nVMs); + if (nVMs > 0) { + jvmBuf[0]->GetEnv((void **) &env, JNI_VERSION_1_6); + return env; + } + JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */ + JavaVMOption* options = new JavaVMOption[1]; + options[0].optionString = (char *) getenv("JVM_OPTIONS"); + vm_args.version = JNI_VERSION_1_6; + vm_args.nOptions = NULL != options[0].optionString ? 1 : 0; + vm_args.options = options; + vm_args.ignoreUnrecognized = false; + /* load and initialize a Java VM, return a JNI interface + * pointer in env */ + JNI_CreateJavaVM(&jvm, + ((void **) (&env)), + ((void *) (&vm_args))); + delete options; + return env; +} + + +static inline jclass getMainClass(); +static inline JNIEnv *getEnv(); + +static jclass getNewMainClass() { + jclass clss = getEnv()->FindClass("Main"); + if (NULL == clss) { + cerr << " Can't find class Main" << endl; + } + return clss; +} + + + +namespace javamain { + + void Main::staticTest(jint i) { + JNIEnv *env =getEnv(); + jclass cls = getMainClass(); + if (cls != NULL) { + jmethodID mid = env->GetStaticMethodID(cls, "staticTest", "(I)V"); + if (NULL == mid) { + std::cerr << "Class Main has no method named staticTest" << std::endl; + } else { + env->CallStaticVoidMethod(cls, mid, i); + } + } + } + + Main::Main(jint i) { + JNIEnv *env =getEnv(); + jclass cls = getMainClass(); + if (cls != NULL) { + jmethodID mid = env->GetMethodID(cls, "", "(I)V"); + if (NULL == mid) { + std::cerr << "Class Main has no method constructor with int parameter" << std::endl; + } else { + jthis = env->NewObject(cls, mid, i); + jobjectRefType ref = env->GetObjectRefType(jthis); +// std::cout << "new ref is " << ref << std::endl; + if(ref != JNIGlobalRefType) { + jthis = env->NewGlobalRef(jthis); + } + } + } + } + jint Main::getI() { + jclass cls = getMainClass(); + JNIEnv *env =getEnv(); + if (cls != NULL) { + jmethodID mid = env->GetMethodID(cls, "getI", "()I"); + if (NULL == mid) { + std::cerr << "Class Main has no method named getI" << std::endl; + return (jint) -1; + } else { + return env->CallIntMethod(jthis, mid); + } + } + } + + Main::~Main() { + if(NULL != jthis) { + getEnv()->DeleteGlobalRef(jthis); + jthis=NULL; + } + } +} + +static JNIEnv *env = NULL; +static jclass mainClass = NULL; + +namespace javamain { + void closeJVM() { + JavaVM * jvmBuf[1]; + jsize nVMs; + env = NULL; + mainClass = NULL; + jint v = JNI_GetCreatedJavaVMs(jvmBuf, 1, &nVMs); + if (nVMs > 0) { + jvmBuf[0]->DestroyJavaVM(); + } + } +} + + + +static inline jclass getMainClass() { + if (mainClass != NULL) { + return mainClass; + } + mainClass = getNewMainClass(); +} + +static inline JNIEnv *getEnv() { + if (env != NULL) { + return env; + } + env = getNewEnv(); + return env; +} \ No newline at end of file diff --git a/CppWrapJavaExampleDynamicLibrary/javamain.h b/CppWrapJavaExampleDynamicLibrary/javamain.h new file mode 100644 index 0000000..27511f3 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/javamain.h @@ -0,0 +1,30 @@ +/* + * File: main.h + * Author: Will Shackleford {@literal } + * + * Created on July 30, 2015, 9:55 AM + */ + +#ifndef MAIN_H +#define MAIN_H + +#include + +namespace javamain { + + class Main { + private: + jobject jthis; + + public: + static void staticTest(jint i); + virtual jint getI(); + Main(jint i); + virtual ~Main(); + }; + + extern void closeJVM(); +} + +#endif /* MAIN_H */ + diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Debug.mk b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Debug.mk new file mode 100644 index 0000000..0b985b7 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Debug.mk @@ -0,0 +1,84 @@ +# +# Generated Makefile - do not edit! +# +# Edit the Makefile in the project folder instead (../Makefile). Each target +# has a -pre and a -post target defined where you can add customized code. +# +# This makefile implements configuration specific macros and targets. + + +# Environment +MKDIR=mkdir +CP=cp +GREP=grep +NM=nm +CCADMIN=CCadmin +RANLIB=ranlib +CC=gcc +CCC=g++ +CXX=g++ +FC=gfortran +AS=as + +# Macros +CND_PLATFORM=GNU-Linux-x86 +CND_DLIB_EXT=so +CND_CONF=Debug +CND_DISTDIR=dist +CND_BUILDDIR=build + +# Include project Makefile +include Makefile + +# Object Directory +OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} + +# Object Files +OBJECTFILES= \ + ${OBJECTDIR}/javamain.o + + +# C Compiler Flags +CFLAGS= + +# CC Compiler Flags +CCFLAGS= +CXXFLAGS= + +# Fortran Compiler Flags +FFLAGS= + +# Assembler Flags +ASFLAGS= + +# Link Libraries and Options +LDLIBSOPTIONS=-L${JRE_LIB_DIR} -ljvm + +# Build Targets +.build-conf: ${BUILD_SUBPROJECTS} + "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} + +${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT}: ${OBJECTFILES} + ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} + ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} ${OBJECTFILES} ${LDLIBSOPTIONS} -shared -fPIC + +${OBJECTDIR}/javamain.o: javamain.cpp + ${MKDIR} -p ${OBJECTDIR} + ${RM} "$@.d" + $(COMPILE.cc) -g -I. -I${JDK_DIR}/include/ -I${JDK_DIR}/include/linux/ -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux -fPIC -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/javamain.o javamain.cpp + +# Subprojects +.build-subprojects: + +# Clean Targets +.clean-conf: ${CLEAN_SUBPROJECTS} + ${RM} -r ${CND_BUILDDIR}/${CND_CONF} + ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} + +# Subprojects +.clean-subprojects: + +# Enable dependency checking +.dep.inc: .depcheck-impl + +include .dep.inc diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Release.mk b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Release.mk new file mode 100644 index 0000000..c715e65 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-Release.mk @@ -0,0 +1,84 @@ +# +# Generated Makefile - do not edit! +# +# Edit the Makefile in the project folder instead (../Makefile). Each target +# has a -pre and a -post target defined where you can add customized code. +# +# This makefile implements configuration specific macros and targets. + + +# Environment +MKDIR=mkdir +CP=cp +GREP=grep +NM=nm +CCADMIN=CCadmin +RANLIB=ranlib +CC=gcc +CCC=g++ +CXX=g++ +FC=gfortran +AS=as + +# Macros +CND_PLATFORM=GNU-Linux-x86 +CND_DLIB_EXT=so +CND_CONF=Release +CND_DISTDIR=dist +CND_BUILDDIR=build + +# Include project Makefile +include Makefile + +# Object Directory +OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} + +# Object Files +OBJECTFILES= \ + ${OBJECTDIR}/javamain.o + + +# C Compiler Flags +CFLAGS= + +# CC Compiler Flags +CCFLAGS= +CXXFLAGS= + +# Fortran Compiler Flags +FFLAGS= + +# Assembler Flags +ASFLAGS= + +# Link Libraries and Options +LDLIBSOPTIONS=-L${JRE_LIB_DIR} -ljvm + +# Build Targets +.build-conf: ${BUILD_SUBPROJECTS} + "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} + +${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT}: ${OBJECTFILES} + ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} + ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} ${OBJECTFILES} ${LDLIBSOPTIONS} -shared -fPIC + +${OBJECTDIR}/javamain.o: javamain.cpp + ${MKDIR} -p ${OBJECTDIR} + ${RM} "$@.d" + $(COMPILE.cc) -O2 -I. -I${JDK_DIR}/include/ -I${JDK_DIR}/include/linux/ -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux -fPIC -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/javamain.o javamain.cpp + +# Subprojects +.build-subprojects: + +# Clean Targets +.clean-conf: ${CLEAN_SUBPROJECTS} + ${RM} -r ${CND_BUILDDIR}/${CND_CONF} + ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} + +# Subprojects +.clean-subprojects: + +# Enable dependency checking +.dep.inc: .depcheck-impl + +include .dep.inc diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-impl.mk b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-impl.mk new file mode 100644 index 0000000..d87a060 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-impl.mk @@ -0,0 +1,133 @@ +# +# Generated Makefile - do not edit! +# +# Edit the Makefile in the project folder instead (../Makefile). Each target +# has a pre- and a post- target defined where you can add customization code. +# +# This makefile implements macros and targets common to all configurations. +# +# NOCDDL + + +# Building and Cleaning subprojects are done by default, but can be controlled with the SUB +# macro. If SUB=no, subprojects will not be built or cleaned. The following macro +# statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf +# and .clean-reqprojects-conf unless SUB has the value 'no' +SUB_no=NO +SUBPROJECTS=${SUB_${SUB}} +BUILD_SUBPROJECTS_=.build-subprojects +BUILD_SUBPROJECTS_NO= +BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} +CLEAN_SUBPROJECTS_=.clean-subprojects +CLEAN_SUBPROJECTS_NO= +CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} + + +# Project Name +PROJECTNAME=CppWrapJavaExampleDynamicLibrary + +# Active Configuration +DEFAULTCONF=Debug +CONF=${DEFAULTCONF} + +# All Configurations +ALLCONFS=Debug Release + + +# build +.build-impl: .build-pre .validate-impl .depcheck-impl + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf + + +# clean +.clean-impl: .clean-pre .validate-impl .depcheck-impl + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf + + +# clobber +.clobber-impl: .clobber-pre .depcheck-impl + @#echo "=> Running $@..." + for CONF in ${ALLCONFS}; \ + do \ + "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ + done + +# all +.all-impl: .all-pre .depcheck-impl + @#echo "=> Running $@..." + for CONF in ${ALLCONFS}; \ + do \ + "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ + done + +# build tests +.build-tests-impl: .build-impl .build-tests-pre + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf + +# run tests +.test-impl: .build-tests-impl .test-pre + @#echo "=> Running $@... Configuration=$(CONF)" + "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf + +# dependency checking support +.depcheck-impl: + @echo "# This code depends on make tool being used" >.dep.inc + @if [ -n "${MAKE_VERSION}" ]; then \ + echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \ + echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ + echo "include \$${DEPFILES}" >>.dep.inc; \ + echo "endif" >>.dep.inc; \ + else \ + echo ".KEEP_STATE:" >>.dep.inc; \ + echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ + fi + +# configuration validation +.validate-impl: + @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ + then \ + echo ""; \ + echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ + echo "See 'make help' for details."; \ + echo "Current directory: " `pwd`; \ + echo ""; \ + fi + @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ + then \ + exit 1; \ + fi + + +# help +.help-impl: .help-pre + @echo "This makefile supports the following configurations:" + @echo " ${ALLCONFS}" + @echo "" + @echo "and the following targets:" + @echo " build (default target)" + @echo " clean" + @echo " clobber" + @echo " all" + @echo " help" + @echo "" + @echo "Makefile Usage:" + @echo " make [CONF=] [SUB=no] build" + @echo " make [CONF=] [SUB=no] clean" + @echo " make [SUB=no] clobber" + @echo " make [SUB=no] all" + @echo " make help" + @echo "" + @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," + @echo " also build subprojects." + @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," + @echo " also clean subprojects." + @echo "Target 'clobber' will remove all built files from all configurations and," + @echo " unless 'SUB=no', also from subprojects." + @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," + @echo " also build subprojects." + @echo "Target 'help' prints this message." + @echo "" + diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-variables.mk b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-variables.mk new file mode 100644 index 0000000..8061cd4 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Makefile-variables.mk @@ -0,0 +1,35 @@ +# +# Generated - do not edit! +# +# NOCDDL +# +CND_BASEDIR=`pwd` +CND_BUILDDIR=build +CND_DISTDIR=dist +# Debug configuration +CND_PLATFORM_Debug=GNU-Linux-x86 +CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-Linux-x86 +CND_ARTIFACT_NAME_Debug=libCppWrapJavaExampleDynamicLibrary.so +CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-Linux-x86/libCppWrapJavaExampleDynamicLibrary.so +CND_PACKAGE_DIR_Debug=dist/Debug/GNU-Linux-x86/package +CND_PACKAGE_NAME_Debug=libCppWrapJavaExampleDynamicLibrary.so.tar +CND_PACKAGE_PATH_Debug=dist/Debug/GNU-Linux-x86/package/libCppWrapJavaExampleDynamicLibrary.so.tar +# Release configuration +CND_PLATFORM_Release=GNU-Linux-x86 +CND_ARTIFACT_DIR_Release=dist/Release/GNU-Linux-x86 +CND_ARTIFACT_NAME_Release=libCppWrapJavaExampleDynamicLibrary.so +CND_ARTIFACT_PATH_Release=dist/Release/GNU-Linux-x86/libCppWrapJavaExampleDynamicLibrary.so +CND_PACKAGE_DIR_Release=dist/Release/GNU-Linux-x86/package +CND_PACKAGE_NAME_Release=libCppWrapJavaExampleDynamicLibrary.so.tar +CND_PACKAGE_PATH_Release=dist/Release/GNU-Linux-x86/package/libCppWrapJavaExampleDynamicLibrary.so.tar +# +# include compiler specific variables +# +# dmake command +ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ + (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) +# +# gmake command +.PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) +# +include nbproject/private/Makefile-variables.mk diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Debug.bash b/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Debug.bash new file mode 100644 index 0000000..e195cc6 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Debug.bash @@ -0,0 +1,76 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=GNU-Linux-x86 +CND_CONF=Debug +CND_DISTDIR=dist +CND_BUILDDIR=build +CND_DLIB_EXT=so +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} +OUTPUT_BASENAME=libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} +PACKAGE_TOP_DIR=libCppWrapJavaExampleDynamicLibrary.so/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/libCppWrapJavaExampleDynamicLibrary.so/lib" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}lib/${OUTPUT_BASENAME}" 0644 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libCppWrapJavaExampleDynamicLibrary.so.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libCppWrapJavaExampleDynamicLibrary.so.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Release.bash b/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Release.bash new file mode 100644 index 0000000..8736f06 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/Package-Release.bash @@ -0,0 +1,76 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=GNU-Linux-x86 +CND_CONF=Release +CND_DISTDIR=dist +CND_BUILDDIR=build +CND_DLIB_EXT=so +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} +OUTPUT_BASENAME=libCppWrapJavaExampleDynamicLibrary.${CND_DLIB_EXT} +PACKAGE_TOP_DIR=libCppWrapJavaExampleDynamicLibrary.so/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/libCppWrapJavaExampleDynamicLibrary.so/lib" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}lib/${OUTPUT_BASENAME}" 0644 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libCppWrapJavaExampleDynamicLibrary.so.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libCppWrapJavaExampleDynamicLibrary.so.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/configurations.xml b/CppWrapJavaExampleDynamicLibrary/nbproject/configurations.xml new file mode 100644 index 0000000..e38cc6c --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/configurations.xml @@ -0,0 +1,103 @@ + + + + + javamain.h + + + + + javamain.cpp + + + + + Makefile + + + Makefile + + + + default + true + false + + + + + . + ${JDK_DIR}/include/ + ${JDK_DIR}/include/linux/ + /usr/lib/jvm/java-7-openjdk-amd64/include + /usr/lib/jvm/java-7-openjdk-amd64/include/linux + + + + + ${JRE_LIB_DIR} + + + -ljvm + + + + + + + + + + + default + true + false + + + + 5 + + + 5 + + . + ${JDK_DIR}/include/ + ${JDK_DIR}/include/linux/ + /usr/lib/jvm/java-7-openjdk-amd64/include + /usr/lib/jvm/java-7-openjdk-amd64/include/linux + + + + 5 + + + 5 + + + + ${JRE_LIB_DIR} + + + -ljvm + + + + + + + + + + diff --git a/CppWrapJavaExampleDynamicLibrary/nbproject/project.xml b/CppWrapJavaExampleDynamicLibrary/nbproject/project.xml new file mode 100644 index 0000000..4884bd5 --- /dev/null +++ b/CppWrapJavaExampleDynamicLibrary/nbproject/project.xml @@ -0,0 +1,28 @@ + + + org.netbeans.modules.cnd.makeproject + + + CppWrapJavaExampleDynamicLibrary + + cpp + h + UTF-8 + + + + + Debug + 2 + + + Release + 2 + + + + false + + + + diff --git a/Main.java b/Main.java deleted file mode 100644 index b873c74..0000000 --- a/Main.java +++ /dev/null @@ -1,7 +0,0 @@ - -public class Main { - - public static void test(int i) { - System.out.println("Main.test("+i+") called."); - } -} diff --git a/Makefile b/Makefile index 9aac3aa..dfc49f9 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,17 @@ -OBJEXT=.o -EXEEXT= -CLASSEXT=.class - -JDK_DIR=${JAVA_HOME} -JAVAC=${JDK_DIR}/bin/javac -PLAT=linux -JRE_TYPE=i386/server -CXXFLAGS+= -fPIC -I ${JDK_DIR}/include/${PLAT} -I ${JDK_DIR}/include/ -JNI_LIB_DIR= ${JDK_DIR}/jre/lib/${JRE_TYPE} -LDFLAGS+= -L ${JNI_LIB_DIR} -ljvm - -all : test -.PHONY: all test clean - -clean: - -\rm Main${CLASSEXT} cppcalljava${EXEEXT} cppcalljava${OBJEXT} - -test : cppcalljava${EXEEXT} Main${CLASSEXT} - env LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${JNI_LIB_DIR} ./cppcalljava${EXEEXT} - -Main.class : Main.java - ${JAVAC} $^ - -cppcalljava${OBJEXT} : cppcalljava.cxx - $(CXX) -c ${CXXFLAGS} $^ -o $@ - -cppcalljava${EXEEXT} : cppcalljava${OBJEXT} - $(CXX) $^ ${CXXFLAGS} ${LDFLAGS} -o $@ - +JDK_DIR=$(shell ./findJavaHome.sh) +JRE_LIB_DIR=$(shell ./findJVMLib.sh) +JAVAC=$(JDK_DIR)/bin/javac + +build clean clobber all: + $(MAKE) -C CppWrapJavaExampleDynamicLibrary JDK_DIR=$(JDK_DIR) JRE_LIB_DIR=$(JRE_LIB_DIR) JAVAC=$(JAVAC) $@ + $(MAKE) -C CppCallJavaApplication JDK_DIR=$(JDK_DIR) JRE_LIB_DIR=$(JRE_LIB_DIR) JAVAC=$(JAVAC) $@ + + +testRun help: + $(MAKE) -C CppCallJavaApplication DK_DIR=$(JDK_DIR) JRE_LIB_DIR=$(JRE_LIB_DIR) JAVAC=$(JAVAC) $@ + +test : testRun + +.PHONY: build clean clobber all help testRun test diff --git a/cppcalljava.cxx b/cppcalljava.cxx deleted file mode 100644 index 2cc24d0..0000000 --- a/cppcalljava.cxx +++ /dev/null @@ -1,35 +0,0 @@ -#include /* where everything is defined */ -#include -#include - -int main(int argc, const char **args) { - JavaVM *jvm; /* denotes a Java VM */ - JNIEnv *env; /* pointer to native method interface */ - JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */ - JavaVMOption* options = new JavaVMOption[1]; - options[0].optionString = (char *) "-Djava.class.path=/usr/lib/java:."; - vm_args.version = JNI_VERSION_1_6; - vm_args.nOptions = 1; - vm_args.options = options; - vm_args.ignoreUnrecognized = false; - /* load and initialize a Java VM, return a JNI interface - * pointer in env */ - JNI_CreateJavaVM(&jvm, - ((void **) (&env)), - ((void *) (&vm_args))); - delete options; - /* invoke the Main.test method using the JNI */ - jclass cls = env->FindClass("Main"); - if(NULL == cls) { - std::cerr << " Can't find class main" << std::endl; - } else { - jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V"); - if(NULL == mid) { - std::cerr << "Class Main has no method named test" << std::endl; - } else { - env->CallStaticVoidMethod(cls, mid, 100); - } - } - /* We are done. */ - jvm->DestroyJavaVM(); -} diff --git a/findJVMLib.sh b/findJVMLib.sh new file mode 100755 index 0000000..9afe68c --- /dev/null +++ b/findJVMLib.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +if test "x${JAVA_HOME}" != "x" ; then + export JDK_DIR="${JAVA_HOME}"; +elif test "x${JDK_DIR}" = "x" ; then + export JDK_DIR=`./findJavaHome.sh`; +fi + +find ${JDK_DIR} -name libjvm.so | sed 's#libjvm.so##' | head -1 diff --git a/findJavaHome.sh b/findJavaHome.sh new file mode 100755 index 0000000..c5fe3d0 --- /dev/null +++ b/findJavaHome.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +if test "x${JAVA_HOME}" != "x" ; then + echo "${JAVA_HOME}"; +else + locate bin/javac | sed 's#bin/javac##' | head -1 +fi \ No newline at end of file