[Assorted-commits] SF.net SVN: assorted:[932] sandbox/trunk/src/java/java-from-c
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-08-07 01:59:28
|
Revision: 932 http://assorted.svn.sourceforge.net/assorted/?rev=932&view=rev Author: yangzhang Date: 2008-08-07 01:59:37 +0000 (Thu, 07 Aug 2008) Log Message: ----------- added (a working) hzrun; tweaks, renames, cleanup Modified Paths: -------------- sandbox/trunk/src/java/java-from-c/Makefile Added Paths: ----------- sandbox/trunk/src/java/java-from-c/Hello.java sandbox/trunk/src/java/java-from-c/hzrun.cc sandbox/trunk/src/java/java-from-c/jnilib.cc sandbox/trunk/src/java/java-from-c/jnitest.cc Removed Paths: ------------- sandbox/trunk/src/java/java-from-c/jnilib.cpp sandbox/trunk/src/java/java-from-c/jnitest.cpp Added: sandbox/trunk/src/java/java-from-c/Hello.java =================================================================== --- sandbox/trunk/src/java/java-from-c/Hello.java (rev 0) +++ sandbox/trunk/src/java/java-from-c/Hello.java 2008-08-07 01:59:37 UTC (rev 932) @@ -0,0 +1,5 @@ +public class Hello { + public static void main(String[] args) { + System.out.println("hello, world!"); + } +} Modified: sandbox/trunk/src/java/java-from-c/Makefile =================================================================== --- sandbox/trunk/src/java/java-from-c/Makefile 2008-08-06 08:10:02 UTC (rev 931) +++ sandbox/trunk/src/java/java-from-c/Makefile 2008-08-07 01:59:37 UTC (rev 932) @@ -1,3 +1,4 @@ +CLASSPATH := . CPATH := $(JAVA_HOME)/include:$(JAVA_HOME)/include/linux:$(CPATH) LIBRARY_PATH := $(JAVA_LIBRARY_PATH) LD_LIBRARY_PATH := $(JAVA_LIBRARY_PATH):. @@ -2,8 +3,12 @@ -all: test +all: test hzrun Hello.class + ./hzrun Hello ./test -dbg: test - gdb test +Hello.class: Hello.java + javac $< +hzrun: hzrun.cc + g++ -g3 -o $@ $^ -ljava + # Two modes of building this. The first is the more direct way for Linux, and @@ -14,22 +19,22 @@ ifeq ($(mode),) -test: JniTest.h jnitest.cpp libjnitest.so - g++ -g3 -o test jnitest.cpp libjnitest.so -ljava +test: jnitest.cc JniTest.h libjnitest.so + g++ -g3 -o $@ $< libjnitest.so -ljava -libjnitest.so: jnilib.cpp JniTest.h - g++ -g3 -shared -fPIC -o libjnitest.so jnilib.cpp +libjnitest.so: jnilib.cc JniTest.h + g++ -g3 -shared -fPIC -o $@ $< else -test: JniTest.h jnitest.cpp libjnitest.so - g++ -g3 -o test jnitest.cpp librealjnitest.so -ljava +test: jnitest.cc JniTest.h libjnitest.so + g++ -g3 -o $@ $< librealjnitest.so -ljava libjnitest.so: librealjnitest.so - g++ -g3 -shared -fPIC -o libjnitest.so librealjnitest.so + g++ -g3 -shared -fPIC -o $@ $< -librealjnitest.so: jnilib.cpp JniTest.h - g++ -g3 -shared -fPIC -o librealjnitest.so jnilib.cpp +librealjnitest.so: jnilib.cc JniTest.h + g++ -g3 -shared -fPIC -o $@ $< endif @@ -39,8 +44,8 @@ JniTest.class: JniTest.java javac JniTest.java -.PHONY: clean +.PHONY: all dbg clean clean: - rm -f test JniTest.h *.so *.class + rm -f test JniTest.h *.so *.class hzrun Added: sandbox/trunk/src/java/java-from-c/hzrun.cc =================================================================== --- sandbox/trunk/src/java/java-from-c/hzrun.cc (rev 0) +++ sandbox/trunk/src/java/java-from-c/hzrun.cc 2008-08-07 01:59:37 UTC (rev 932) @@ -0,0 +1,97 @@ +#include <iostream> +#include <string> +#include <jni.h> +#include <cassert> +#include <cstring> +#include <cstdlib> + +using namespace std; + +int main(int argc, char* argv[]) { + string classname = "com/horizontica/TPCCServer"; + if (argc != 2) { + cout << "Usage: hzrun javaclassname" << endl; + } else { + classname = argv[1]; + } + + JavaVMOption options[2]; + JavaVMInitArgs vm_args; + JavaVM *jvm; + JNIEnv *env; + long status; + + cout << "CLASSPATH: " << getenv("CLASSPATH") << endl; + options[0].optionString = strdup((string("-Djava.class.path=") + string(getenv("CLASSPATH"))).c_str()); + options[1].optionString = strdup("-verbose:jni"); + + memset(&vm_args, 0, sizeof(vm_args)); + vm_args.version = JNI_VERSION_1_6; + vm_args.nOptions = 1; + vm_args.options = options; + + status = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args); + if (status == JNI_ERR) { + fprintf(stderr, "Error creating VM\n"); + return 1; + } + + jclass class_MainClass; + + jthrowable e; + cout << "Looking for class: " << classname << endl; + class_MainClass = env->FindClass(classname.c_str()); + e = env->ExceptionOccurred(); + if (e) { + cout << "\n**********************exception found****************\n"; + env->ExceptionDescribe(); + return 1; + } else { + cout << "Found class\n"; + } + + assert(class_MainClass != NULL); + jmethodID id_main = env->GetStaticMethodID(class_MainClass, "main", "([Ljava/lang/String;)V"); + e = env->ExceptionOccurred(); + if (e) { + cout << "\n**********************exception found****************\n"; + env->ExceptionDescribe(); + return 1; + } else { + cout << "Found main method\n"; + } + + assert(id_main != NULL); + jclass class_String = env->FindClass("java/lang/String"); + e = env->ExceptionOccurred(); + if (e) { + cout << "\n**********************exception found****************\n"; + env->ExceptionDescribe(); + return 1; + } else { + cout << "Found string\n"; + } + + jobjectArray array_s = env->NewObjectArray(0, class_String, NULL); + e = env->ExceptionOccurred(); + if (e) { + cout << "\n**********************exception found****************\n"; + env->ExceptionDescribe(); + return 1; + } else { + cout << "Created Array\n"; + } + + env->CallStaticVoidMethod(class_MainClass, id_main, array_s); + e = env->ExceptionOccurred(); + if (e) { + cout << "\n**********************exception found****************\n"; + env->ExceptionDescribe(); + return 1; + } else { + cout << "called main\n"; + } + + cout << "success" << endl; +} + Copied: sandbox/trunk/src/java/java-from-c/jnilib.cc (from rev 931, sandbox/trunk/src/java/java-from-c/jnilib.cpp) =================================================================== --- sandbox/trunk/src/java/java-from-c/jnilib.cc (rev 0) +++ sandbox/trunk/src/java/java-from-c/jnilib.cc 2008-08-07 01:59:37 UTC (rev 932) @@ -0,0 +1,15 @@ +#include "JniTest.h" +#include <iostream> +#include <jni.h> +#include <cassert> +using namespace std; + +JNIEXPORT void JNICALL Java_JniTest_printNativeValue(JNIEnv *env, jobject obj, jint x) { + for (int i = 0; i < 1000000; i++) + double x = 13 / 5.5; + if (x % 1000 == 0) { + cout << "native: " << x << endl; + cout.flush(); + } +} + Deleted: sandbox/trunk/src/java/java-from-c/jnilib.cpp =================================================================== --- sandbox/trunk/src/java/java-from-c/jnilib.cpp 2008-08-06 08:10:02 UTC (rev 931) +++ sandbox/trunk/src/java/java-from-c/jnilib.cpp 2008-08-07 01:59:37 UTC (rev 932) @@ -1,15 +0,0 @@ -#include "JniTest.h" -#include <iostream> -#include <jni.h> -#include <cassert> -using namespace std; - -JNIEXPORT void JNICALL Java_JniTest_printNativeValue(JNIEnv *env, jobject obj, jint x) { - for (int i = 0; i < 1000000; i++) - double x = 13 / 5.5; - if (x % 1000 == 0) { - cout << "native: " << x << endl; - cout.flush(); - } -} - Copied: sandbox/trunk/src/java/java-from-c/jnitest.cc (from rev 931, sandbox/trunk/src/java/java-from-c/jnitest.cpp) =================================================================== --- sandbox/trunk/src/java/java-from-c/jnitest.cc (rev 0) +++ sandbox/trunk/src/java/java-from-c/jnitest.cc 2008-08-07 01:59:37 UTC (rev 932) @@ -0,0 +1,74 @@ +#include "JniTest.h" +#include <cstdio> +#include <iostream> +#include <jni.h> +#include <cassert> + +using namespace std; + +int main(int argc, char* argv[]) { + + Java_JniTest_printNativeValue(NULL, NULL, 5); + + JavaVMOption options[2]; + JavaVMInitArgs vm_args; + JavaVM *jvm; + JNIEnv *env; + long status; + + options[0].optionString = strdup("-Djava.class.path=."); + options[1].optionString = strdup("-verbose:jni"); + //options[2].optionString = strdup("-Xverbose:class"); + + memset(&vm_args, 0, sizeof(vm_args)); + vm_args.version = JNI_VERSION_1_6; + vm_args.nOptions = 1; + vm_args.options = options; + + status = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args); + if (status == JNI_ERR) + { + fprintf(stderr, "Error creating VM\n"); + return 1; + } + + jclass class_JniTest; + + jthrowable e; + class_JniTest = env->FindClass("JniTest"); + e = env->ExceptionOccurred(); + if(e) { + cout<<"\n**********************exception found****************\n"; + env->ExceptionDescribe(); + } else { + cout<<"Found class\n"; + } + + + assert(class_JniTest != NULL); + jmethodID id_print = env->GetMethodID(class_JniTest, "printValue", "(I)V"); + e = env->ExceptionOccurred(); + if(e) { + cout<<"\n**********************exception found****************\n"; + env->ExceptionDescribe(); + } else { + cout<<"Found method 1\n"; + } + jmethodID id_construct = env->GetMethodID(class_JniTest, "<init>", "()V"); + e = env->ExceptionOccurred(); + if(e) { + cout<<"\n**********************exception found****************\n"; + env->ExceptionDescribe(); + } else { + cout<<"Found method 2\n"; + } + jobject obj = env->NewObject(class_JniTest, id_construct); + for (int i = 0; i < 100000; i++) + env->CallVoidMethod(obj, id_print, i); + + cout << "This is some output." << endl; + + jvm->DestroyJavaVM(); + return 0; +} + Deleted: sandbox/trunk/src/java/java-from-c/jnitest.cpp =================================================================== --- sandbox/trunk/src/java/java-from-c/jnitest.cpp 2008-08-06 08:10:02 UTC (rev 931) +++ sandbox/trunk/src/java/java-from-c/jnitest.cpp 2008-08-07 01:59:37 UTC (rev 932) @@ -1,78 +0,0 @@ -#include "JniTest.h" -#include <cstdio> -#include <iostream> -#include <jni.h> -#include <cassert> - -using namespace std; - -int main(int argc, char* argv[]) { - - Java_JniTest_printNativeValue(NULL, NULL, 5); - - JavaVMOption options[2]; - JavaVMInitArgs vm_args; - JavaVM *jvm; - JNIEnv *env; - long status; - - options[0].optionString = strdup("-Djava.class.path=."); - options[1].optionString = strdup("-verbose:jni"); - //options[2].optionString = strdup("-Xverbose:class"); - - memset(&vm_args, 0, sizeof(vm_args)); - vm_args.version = JNI_VERSION_1_6; - vm_args.nOptions = 2; - vm_args.options = options; - - status = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args); - if (status == JNI_ERR) - { - fprintf(stderr, "Error creating VM\n"); - return 1; - } - - jclass class_JniTest; - jint value; - - jthrowable e; - class_JniTest = env->FindClass("JniTest"); - e = env->ExceptionOccurred(); - if(e) { - cout<<"\n**********************exception found****************\n"; - env->ExceptionDescribe(); - } - else{ - cout<<"Found class\n"; - } - - - assert(class_JniTest != NULL); - jmethodID id_print = env->GetMethodID(class_JniTest, "printValue", "(I)V"); - e = env->ExceptionOccurred(); - if(e) { - cout<<"\n**********************exception found****************\n"; - env->ExceptionDescribe(); - } - else{ - cout<<"Found method 1\n"; - } - jmethodID id_construct = env->GetMethodID(class_JniTest, "<init>", "()V"); - e = env->ExceptionOccurred(); - if(e) { - cout<<"\n**********************exception found****************\n"; - env->ExceptionDescribe(); - } - else{ - cout<<"Found method 2\n"; - } - jobject obj = env->NewObject(class_JniTest, id_construct); - for (int i = 0; i < 100000; i++) - env->CallVoidMethod(obj, id_print, i); - - cout << "This is some output." << endl; - - jvm->DestroyJavaVM(); - return 0; -} - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |