Calling SWT from C++ code

Praful bhoyar Praful.bhoyar@lntinfotech.com
Fri Nov 5 10:32:00 GMT 2004



Hi All,

      Is it possible to call SWT class in java from C++ using CNI,

What i want to do

1. Write a class in JAVA that uses SWT to draw a screen -- This i have done
and work fine
2. Create a CNI header for this .java file and include in my C++ class, and
try to access the java class methods
3. Compile it using g++ -- compilation fails

my java class

[Phone.java]

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
/*
 * Created on Nov 2, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**

 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Phone extends Composite {

      private KeyPad Kpd = null;
      private MobileSimu pScr = null;

      public static void main(String[] args) {
            /* Before this is run, be sure to set up the following in the
launch configuration
             * (Arguments->VM Arguments) for the correct SWT library path.
             * The following is a windows example:
             * -Djava.library.path
="installation_directory\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86"
             */

            org.eclipse.swt.widgets.Display display =
org.eclipse.swt.widgets.Display.getDefault();
            org.eclipse.swt.widgets.Shell shell = new
org.eclipse.swt.widgets.Shell(display);
            shell.setLayout(new org.eclipse.swt.layout.FillLayout());
            shell.setSize(new org.eclipse.swt.graphics.Point(185,570));
            Phone thisClass = new Phone(shell, org.eclipse.swt.SWT.NONE);
            shell.open();
            thisClass.setText(new String("Dummy"));

            while (!shell.isDisposed()) {
                  if (!display.readAndDispatch()) display.sleep ();
            }
            display.dispose();
      }

      public Phone(Composite parent, int style) {
            super(parent, style);
            initialize();
      }
      private void initialize() {

             Kpd = new KeyPad(this,SWT.NONE);
             pScr = new MobileSimu(this,SWT.NONE);

             this.setBackground(org.eclipse.swt.widgets.Display.getDefault
().getSystemColor(org.eclipse.swt.SWT.COLOR_GRAY));
             this.setBounds(new
org.eclipse.swt.graphics.Rectangle(0,0,200,460));

      }
      public void setText(String pString)
      {
            pScr.setText(pString);
      }


}


[Phone.h]

// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-

#ifndef __Phone__
#define __Phone__

#pragma interface

#include <org/eclipse/swt/widgets/Composite.h>
#include <gcj/array.h>

extern "Java"
{
  class Phone;
  namespace org
  {
    namespace eclipse
    {
      namespace swt
      {
        namespace widgets
        {
          class Composite;
        }
      }
    }
  }
  class MobileSimu;
  class KeyPad;
}

class Phone : public ::org::eclipse::swt::widgets::Composite
{
public:
  static void main (JArray< ::java::lang::String *> *);
  Phone (::org::eclipse::swt::widgets::Composite *, jint);
private:
  void initialize ();
public:
  virtual void setText (::java::lang::String *);
private:
  ::KeyPad * __attribute__((aligned
(__alignof__( ::org::eclipse::swt::widgets::Composite )))) Kpd;
  ::MobileSimu *pScr;
public:

  static ::java::lang::Class class$;
};

#endif /* __Phone__ */


[callJH.cpp]



// we want to use the CNI
#include <gcj/cni.h>
#include <gcj/array.h>

// we want to use some java classes
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include "../MobileSimu/Phone.h"
#include <java/lang/Throwable.h>
#include <java/lang/reflect/Method.h>
#include <java/lang/reflect/Modifier.h>
#include <java/lang/reflect/Array.h>
#include <java/util/Hashtable.h>


 using namespace java::lang;

int main(void)
{
      JvCreateJavaVM( NULL);
      JvAttachCurrentThread( NULL, NULL);

      Phone* obj = new Phone();
      obj->setText(JvNewStringLatin1("from c++"));





    JvDetachCurrentThread();

}



i am compling this as

g++ -c callJH.cpp

errors i get are

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\716185>d:

D:\>cd wsp

D:\wsp>ls
MobileSimu  Testing  callJava  jniTesting  tempC
Project     callJH   demo1     sampleJava


D:\wsp\callJH>g++ callJH.cpp
In file included from callJH.cpp:10:
../MobileSimu/Phone.h:8:47: org/eclipse/swt/widgets/Composite.h: No such
file or
 directory
In file included from callJH.cpp:10:
../MobileSimu/Phone.h:32: error: invalid use of undefined type `struct
org::ecli
pse::swt::widgets::Composite'
../MobileSimu/Phone.h:22: error: forward declaration of `struct
org::eclipse::sw
t::widgets::Composite'
../MobileSimu/Phone.h:41: error: invalid application of `__alignof__' to an
inco
mplete type
../MobileSimu/Phone.h:41: error: requested alignment is not a power of 2
callJH.cpp: In function `int main()':
callJH.cpp:25: error: no matching function for call to `Phone::Phone()'
../MobileSimu/Phone.h:35: note: candidates are:
Phone::Phone(org::eclipse::swt::
widgets::Composite*, jint)


Can somebody help



Thanks and Regards

Praful Bhoyar





Thanks and Regards

Praful Bhoyar
L&T Infotech
Navi Mumbai
0091-22-55934125

************************************
The information contained in this email has been classified:
[x] L&T Infotech General Business  [ ] L&T Infotech Internal Use
[ ] L&T Infotech Confidential            [ ] L&T Infotech Proprietary



This email may contain confidential or privileged information for the
intended recipient(s). If you are not the intended recipient, please do not
use or disseminate the information, notify the sender and delete it from
your system.  Thanks



______________________________________________________________________



More information about the Java mailing list