Use a Java class (PB9)Tag(s): Powerscript
We simply need to build an "EJB proxy" to our class even if it's not an EJB.
Consider the following simple class :
[Simple.java] public class Simple { public Simple() { } public static String sayHello(){ return "Hello world"; } public String sayNonStaticHello(){ return "Hello world"; } public static void main(String[] args) { System.out.println(Simple.sayHello()); } }
[SimpleHome.java] public interface SimpleHome { }
- Start Powerbuilder.
- Create a new application, call it "pbjsimple", put everything in the same directory(.class and .pbl).
- Start the EJB Proxies Wizard, Type "Simple" as the component and the classpath is the folder containing the "Simple.class" file.
- Run the project, Powerbuilder will generate proxies for your classes and also for most standard Java classes!
- Next, open a window from the application and drop a Button and insert
the following Powerscript in the "click event".
Note : AFAIK Java static methods are not visible from Powerbuilder.
JavaVM lJavaVM EJBConnection lEJBConn Simple lnv_simple long ll_return lJavaVM = CREATE JavaVM // need to specify the classpath form the Simple class ll_return = & lJavaVM.CreateJavaVM("C:\Applications\rega\dev\Work\pb9\Target3", FALSE) CHOOSE CASE ll_return CASE 1 CASE 0 CASE -1 MessageBox ( "", "jvm.dll was not found in the classpath.") CASE -2 MessageBox ( "", "pbejbclient90.jar file was not found." ) CASE ELSE MessageBox ( "", "Unknown result (" + String (ll_return ) + ")" ) END CHOOSE IF ll_return < 0 THEN DESTROY lJavaVM END IF lEJBConn = CREATE EJBConnection ll_return = lEJBConn.CreateJavaInstance( lnv_simple, "Simple") IF ll_return <> 0 THEN MessageBox("", "CreateJavaInstance returned " +string(ll_return)) destroy lEJBConn ELSE TRY MessageBox("From Java!" , lnv_simple.sayNonStaticHello()) CATCH (CreateException ce) MessageBox( "Create Exception", ce.getMessage() ) CATCH (Throwable t) MessageBox(" Other Exception", t.getMessage()) END TRY END IF
You can download this example here