Professional Documents
Culture Documents
net
Java
.NET Java C#
Java (managed code byte code) Java
C#(byte code managed code)
Java :
:MyToolkit.java
C++?
C++ Java Java
:
C/C++(.exe)
4.(JNI)
1.
Java(
Java(jvm.dll)
2.(JNI) 3.(JNI)
(.class)
C/C++
Java Java Windows
(.dll, dynamic linking library)
jvm.dll
(process)(attach) jvm.dll
Java
explicit Win32 API LoadLibrary()
GetProcAddress() implicit
(.h)(.lib)
(.h)(.lib)
JNI_CreateJavaVM() Java
Java
JNI_DestroyJavaVM() Java
:
Java
C/C++(.exe)
1.jvm.dll
3.
JNI_DestroyJavaVM()
2.
JNI_CreateJavaVM
Java(
Java(jvm.dll)
step by step
Visual Studio.NET
Visual Studio.NET :
Visual Studio.NET
(Solution)
(Project)
C++(Item)
Solution File /
New / Blank Solution:
Name (Solution)(
JavaSolution) Location
:
OK
Solution(Project) Solution Explorer
Solution Add / New
Project
Visual Studio.NET
C++(.cpp)
Solution Explorer Project
Add / Add New Item:
:Main.cpp
#include<iostream>
#include<jni.h>
int main()
JavaVM* jvm ;
JNIEnv* env ;
JavaVMOption options[1] ;
JavaVMInitArgs vmargs ;
long status ;
options[0].optionString = "-Djava.class.path=.";
vmargs.version = JNI_VERSION_1_2;
vmargs.options = options;
vmargs.nOptions = 1;
status = JNI_CreateJavaVM(&jvm,(void**)&env,&vmargs) ;
if(status != JNI_OK)
{
cout << "Java/" << endl ;
return 1 ;
}
cout << "Java" ;
jvm->DestroyJavaVM();
return 0 ;
OK
implicit
(linking)
:
(linker).lib
Visual Studio.NET Tools / Options Options
Options Projects VC++ Directories
Show directories for Library files <jdk
>\lib Java 2 SDK 1.3.1 d:\jdk1.3.1
d:\jdk1.3.1\lib
lib
.lib Solution
Provider Project Properties
JNIProject Property Pages Linker
Input Additional Dependences jvm.lib
:
OK
:
(.h)(.lib) JNIProject
Property Pages
:
C/C++ General Additional Include Directories
:
Linker General Additional Library Directories :
Java
:
implicit
Java jvm.dll
( PATH )
jvm.dll:jvm.dll ?
Windows Programmer
Windows jvm.dll jvm.dll
( Java 2 SDK 1.3.1
4 Java 2 SDK 1.4.0 RC
3 )
jvm.dll ?
try and error jvm.dll
jvm.dll :
Java Java
?
Java
java.exe JRE Java (jvm.dll)
jvm.dll jvm.dll explicit (
Win32 API LoadLibrary() GetProcAddress())
jvm.dll
jvm.dll
: jvm.dll
<JRE >\bin
?
!! PATH JRE jvm.dll
:
jvm.dll Windows
Java
:
Java :
:Main.cpp
#include<iostream>
#include<jni.h>
int main()
JavaVM* jvm ;
JNIEnv* env ;
JavaVMOption options[2] ;
JavaVMInitArgs vmargs ;
long status ;
options[0].optionString = "-Djava.class.path=.";
options[1].optionString = "-verbose:jni";
vmargs.version = JNI_VERSION_1_2;
vmargs.options = options;
vmargs.nOptions = 2;
status = JNI_CreateJavaVM(&jvm,(void**)&env,&vmargs) ;
if(status != JNI_OK)
{
cout << "Java/" << endl ;
return 1 ;
}
cout << "Java" ;
jvm->DestroyJavaVM();
return 0 ;
}
jvm.dll
:
(: verbose:jni Xcheck:jni )
Java
Java
:
:Main.cpp
#include<iostream>
#include<jni.h>
int main()
JavaVM* jvm ;
JNIEnv* env ;
JavaVMOption options[1] ;
JavaVMInitArgs vmargs ;
long status ;
options[0].optionString = "-Djava.class.path=D:\\java_and_vsdotnet\\java";
vmargs.version = JNI_VERSION_1_2;
vmargs.options = options;
vmargs.nOptions = 1;
status = JNI_CreateJavaVM(&jvm,(void**)&env,&vmargs) ;
if(status != JNI_OK)
{
cout << "Java/" << endl ;
return 1 ;
}
cout << "Java" << endl;
jclass toolclass ;
toolclass = env->FindClass("MyToolkit") ;
if(toolclass == NULL)
{
cout << "MyToolkit" ;
return 1 ;
jmethodID function ;
function = env->GetStaticMethodID(toolclass,"function1","(Ljava/lang/String;)V") ;
if(function == NULL)
{
cout << "function1" ;
return 1 ;
return 0 ;
java.class.path MyToolkit.class
:
MyToolkit.class:
function1 UTF
:
Java
:
GetStaticMethodID
UltraEdit MyToolkit.class :
:
Java C++
C++ Java Java
Java Java