delphi调用java代码

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,jni;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  vm_args: Jni.JavaVMInitArgs;
  options: array[0..2] of Jni.JavaVMOption;
  jvm: Jni.PJavaVM;
  env: Jni.PJNIEnv;
  rec: Integer;
  cid: Jni.JClass;
  mid: Jni.JMethodID;
  tmp: Jni.JString;
  tenv: Jni.TJNIEnv;
begin
  options[0].optionString := '-Djava.compiler=NONE';
  options[1].optionString := '-Djava.class.path=.;C:\Documents and Settings\Administrator\桌面\1111';
 // options[1].optionString := '-Djava.class.path=.;C:\Documents and Settings\Administrator\桌面\1111' ;
{  options[1].optionString := '-Djava.class.path=.;c:\\;.\\CDemo\\bin;.\\lib\\Aa1.jar;.\\lib\\antlr-2.7.6.jar;.\\lib\\asm.jar;.\\lib\\asm-attrs.jar;.\\lib\\cglib-'+
                             '2.1.3.jar;.\\lib\\charsets.jar;.\\lib\\commons-beanutils-1.7.0.jar;.\\lib\\commons-codec-1.3.jar;.\\lib\\commons-collections-2.1.1.jar;.\\lib\\commons-collections-'+
                             '3.2.jar;.\\lib\\commons-dbcp-1.2.1.jar;.\\lib\\commons-httpclient-3.0.jar;.\\lib\\commons-io-1.3.2.jar;.\\lib\\commons-lang-2.0.jar;.\\lib\\commons-'+
                             'logging.jar;.\\lib\\commons-logging-1.0.4.jar;.\\lib\\commons-pool-1.2.jar;.\\lib\\dnsns.jar;.\\lib\\dom4j-1.6.1.jar;.\\lib\\ehcache-1.2.3.jar;.\\lib\\ejb3-'+
                             'persistence.jar;.\\lib\\ezmorph-1.0.5.jar;.\\lib\\freemarker-2.3.8.jar;.\\lib\\hibernate3.jar;.\\lib\\hibernate-annotations.jar;.\\lib\\hibernate-commons-'+
                             'annotations.jar;.\\lib\\hibernate-entitymanager.jar;.\\lib\\hibernate-validator.jar;.\\lib\\jaas.jar;.\\lib\\jacob.jar;.\\lib\\javassist.jar;.\\lib\\jaxen-1.1-beta-'+
                             '7.jar;.\\lib\\jboss-archive-browsing.jar;.\\lib\\jce.jar;.\\lib\\jdom-1.0.jar;.\\lib\\json-lib-2.3-jdk15.jar;.\\lib\\jsonplugin-0.32.jar;.\\lib\\jsse.jar;.\\lib\\jstl-'+
                             '1.2.jar;.\\lib\\jta.jar;.\\lib\\junit.jar;.\\lib\\localedata.jar;.\\lib\\log4j-1.2.11.jar;.\\lib\\log4j-1.2.14.jar;.\\lib\\mysql-connector-java-5.0.8-'+
                             'bin.jar;.\\lib\\mysql-connector-java-5.1.10-bin.jar;;.\\lib\\ognl-'+
                             '2.6.11.jar;.\\lib\\org.eclipse.swt.win32.win32.x86_3.5.2.v3557f.jar;.\\lib\\pinyin4j-2.5.0.jar;.\\lib\\resources.jar;.\\lib\\rt.jar;.\\lib\\servlet-'+
                             'api.jar;.\\lib\\spring.jar;.\\lib\\spring-1.2.6.jar;.\\lib\\struts2-core-2.0.6.jar;.\\lib\\struts2-spring-plugin-'+
                             '2.0.6.jar;.\\lib\\sunjce_provider.jar;.\\lib\\sunmscapi.jar;.\\lib\\sunpkcs11.jar;.\\lib\\xbean-2.2.0.jar;.\\lib\\xbean-spring-2.8.jar;.\\lib\\xerces-'+
                             '2.6.2.jar;.\\lib\\xml-apis.jar;.\\lib\\xwork-2.0.1.jar';  }
  options[2].optionString := '-verbose:jni';
  JNI_GetDefaultJavaVMInitArgs(@vm_args);
  vm_args.version := Jni.JNI_VERSION_1_4;
  vm_args.options := @options;
  vm_args.nOptions := 3;
  vm_args.ignoreUnrecognized := Jni.JNI_TRUE;
  rec := Jni.JNI_CreateJavaVM(@jvm, @env, @vm_args);
  if (rec = JNI_ERR ) then begin
    Windows.MessageBox(Self.Handle, '不能创建 JVM', 'ERR', Windows.MB_OK);
    exit;
  end;
  tenv := TJNIEnv.Create(env);
  cid := tenv.FindClass('HlCheck');
  if (cid = nil) then begin
    Windows.MessageBox(Self.Handle, '找不到类:T', 'ERR', Windows.MB_OK);
    exit;
  end;
  mid := tenv.GetStaticMethodID(cid, 'dTest', '(Ljava/lang/String;)Ljava/lang/String;');
  if (tenv.ExceptionOccurred<>nil) then begin
    Windows.MessageBox(Self.Handle, '找不到方法:dTest', 'ERR', Windows.MB_OK);
    exit;
  end;
  tmp := tenv.CallStaticObjectMethod(cid, mid, [edit1.Text]);//参数array of const
  if (tenv.ExceptionOccurred()<>nil) then begin
    Windows.MessageBox(Self.Handle, '调用方法失败!', 'ERR', Windows.MB_OK);
    exit;
  end;
  Windows.MessageBox(Self.Handle, pchar(tenv.UnicodeJStringToString(tmp)), '成功了', Windows.MB_OK);
  tenv.Free();
  jvm^.DestroyJavaVM(jvm);//释放JVM资源
end;

end.


你可能感兴趣的:(java,jvm,Hibernate,struts,jni,Delphi)