You are on page 1of 19

Visual Studio.

net
Java



Borland C++ Builder JNI Java


Microsoft Office C++
Windows Programming Borland C++
Builder Microsoft Visual C++
Microsoft Visual Studio.NET
JNI Java Java
C++
?
Java JNI C++ Java ?
Java C++?
C++ Java
.NET Java Microsoft 2002 1
Visual Studio.NET
Visual Studio.NET C++ Java 2 SDK/JRE 1.3.x
Java 2 SDK/JRE 1.4.x Java (Java Virtual
Machine) Java C++
Java

.NET Java C#
Java (managed code byte code) Java
C#(byte code managed code)

Java :
:MyToolkit.java

1 public class MyToolkit


2 {
3 public static void function1(String param)
4 {
5 System.out.println("You input:" + param) ;
6 }
7 }
8

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

Add New Project Project Types Visual C++


Projects Templates Win32 Project Name
Project ( JNIProject):
OK
Win32 (Win32 Application
Wizard) Application Settings Application type
Console application Additional options Empty
project:

Visual Studio.NET

C++(.cpp)
Solution Explorer Project
Add / Add New Item:

Add New Item Templates C++


File(.cpp) Name ( Main.cpp) OK
Visual Studio.NET
Solution Explorer Main.cpp:

:Main.cpp
#include<iostream>

#include<jni.h>

using namespace std ;

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 ;

cout << " : " << status ;

return 1 ;

}
cout << "Java" ;

jvm->DestroyJavaVM();

return 0 ;

Visual Studio.NET Build / Build Solution


jni.h
Visual Studio.NET Tools / Options Options
:
Options Projects VC++
Directories Show directories for Include files
<jdk >\include <jdk >\include\win32
Java 2 SDK 1.3.1 d:\jdk1.3.1
d:\jdk1.3.1\include d:\jdk1.3.1\include\win32 (:jni.h
<jdk >\include\win32 jni_md.h<jdk
>\include\win32 jni_md.h )
:

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 :

Visual Studio.NET Tools / Options Options


Tools / Options Options
JNIProject Property Pages
(:JNI Project)

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

Hotspot Client Virtual Machine PATH


<JRE >\bin\hotspot JDK JRE
JDK d:\jdk1.3.1\:
path=%path%;d:\jdk1.3.1\jre\bin\hotspot

:
jvm.dll Windows

Java
:
Java :
:Main.cpp
#include<iostream>

#include<jni.h>

using namespace std ;

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 ;

cout << " : " << status ;

return 1 ;

}
cout << "Java" ;

jvm->DestroyJavaVM();

return 0 ;
}

jvm.dll
:

(: verbose:jni Xcheck:jni )

Java
Java
:
:Main.cpp
#include<iostream>

#include<jni.h>

using namespace std ;

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 ;

cout << " : " << status << endl ;

return 1 ;

}
cout << "Java" << endl;

jclass toolclass ;

toolclass = env->FindClass("MyToolkit") ;

if(toolclass == NULL)

{
cout << "MyToolkit" ;

return 1 ;

jstring jstr = env->NewStringUTF("My first class!");

jmethodID function ;

function = env->GetStaticMethodID(toolclass,"function1","(Ljava/lang/String;)V") ;

if(function == NULL)

{
cout << "function1" ;

return 1 ;

env->CallStaticVoidMethod(toolclass, function, jstr);


jvm->DestroyJavaVM();

return 0 ;

java.class.path MyToolkit.class
:

MyToolkit.class:

MyToolkit.class (static method)

function1 UTF
:

Java
:

GetStaticMethodID
UltraEdit MyToolkit.class :
:

Java C++
C++ Java Java
Java Java

You might also like