home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native1.1 / implementing / example-1.1 / Main.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  101 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. class Main {
  18.     static MyTest mytest;
  19.     public static void main(String[] args) {
  20.  
  21.         int length = 15;
  22.         int ret;
  23.         int i;
  24.         String teststring = "This is a test";
  25.         int len = 14;
  26.  
  27.         int[] arrayOfInts = new int[10];
  28.         int[] newArray;
  29.         double[] arrayOfDbls = new double[5];
  30.  
  31.         mytest = new MyTest();
  32.  
  33.         //test getLine routine
  34.         StringBuffer sb = new StringBuffer("Testing getLine routine.");
  35.         String s = new String();
  36.         s = sb.toString();
  37.         ret = mytest.getLine(s, sb.length());
  38.         if (ret == -1) {
  39.            System.out.println("Error in getLine routine");
  40.         }    
  41.         ret = mytest.getLine(teststring, teststring.length());
  42.         if (ret == -1) {
  43.            System.out.println("Error in getLine routine");
  44.         }    
  45.         System.out.println ("Return value from getLine: " + ret);
  46.  
  47.         // test array routine 
  48.         for (i = 0; i< arrayOfInts.length;i++) {
  49.            arrayOfInts[i] = i;
  50.         }
  51.         for (i=0; i<arrayOfDbls.length; i++) {
  52.            arrayOfDbls[i] = 3.33 * i;
  53.         }
  54.         ret = 0;
  55.         ret = mytest.getIntValue(arrayOfInts, arrayOfDbls);
  56.         System.out.println("Return value ret: " + ret);
  57.  
  58. // create new array to size of arrayOfInts
  59.  
  60.         newArray = mytest.createArray(arrayOfInts);
  61.         
  62.             for (i = 0; i < newArray.length; i++) {
  63.            System.out.println("new array: " + newArray[i]);
  64.         }
  65.  
  66.         // call native method to get info
  67.         i = mytest.getInfo();
  68.         System.out.println("Value of i: " + i);
  69.  
  70.         ret = mytest.myInt;
  71.         System.out.println("Value of myInt: " + ret);
  72.         System.out.println("Value of myDbl: " + mytest.myDbl);
  73.         System.out.println("Value of myFlt: " + mytest.myFlt);
  74.  
  75.         ret = mytest.createString(teststring, len);
  76.  
  77.         System.out.println("Check for exceptions");
  78.         ret = 0;
  79.         for (i = 98; i<102; i++) {
  80.            try {
  81.             ret = mytest.handleError(i);
  82.            } catch (IllegalArgumentException e) {
  83.             System.out.println("illegal arg. exception occurred");
  84.             System.out.println("Message: " + e.getMessage());
  85.            } catch (Throwable e) {
  86.             System.out.println("Caught an error");
  87.             System.out.println("Message: " + e.getMessage());
  88.            }
  89.         }
  90.         ret = 0;
  91.         try {
  92.                ret = mytest.handleError2();
  93.             System.out.println("Return value from error2: "
  94.                     + ret);
  95.         } catch (Throwable e) {
  96.             System.out.println("Caught error in second routine");
  97.             System.out.println("Return value: " + ret);
  98.         }
  99.     }
  100. }
  101.