2 Replies Latest reply on Jun 19, 2008 7:47 PM by seyi103

    Java Object Instantiation

    seyi103
      I'm having problems using a method within a java class that has been created within my CF code. I don't know Java, but from the little I've learnt the Java code has a constructor. When I call the method within the java class without using the CF init() it throws an Object Instantiation Exception as below:
      ===================================================================================
      An exception occurred while instantiating a Java object. The class must not be an interface or an abstract class. If the class has a constructor that accepts an argument, you must call the constructor explicitly using the init(args) method. Error : Class coldfusion.runtime.java.JavaProxy can not access a member of class com.adeptengr.EncryptDecrypt.StringEncryptWrapper with modifiers
      ===================================================================================

      however when I do use the init() it throws the following error:
      ===================================================================================
      Unable to find a constructor for class com.adeptengr.EncryptDecrypt.StringEncryptWrapper that accepts parameters of type ( '' ).
      ===================================================================================
      As seen in the java code below, the default constructor doesn't accept any argument, and the method that needs to be called in the encrypt method. Please is there anyone who can help or point me in the right direction? Thanks

      Java Code:
      ===================================================================================
      public class StringEncryptWrapper
      {

      StringEncryptWrapper()
      {
      try
      {
      myEncrypter = new StringEncrypter("DES");
      }
      catch(StringEncrypter.EncryptionException e)
      {
      e.printStackTrace();
      }
      }

      public String encrypt(String stringToBeEncrypted)
      throws StringEncrypter.EncryptionException
      {
      return myEncrypter.encrypt(stringToBeEncrypted);
      }

      public String decrypt(String stringToBeDecrypted)
      throws StringEncrypter.EncryptionException
      {
      return myEncrypter.decrypt(stringToBeDecrypted);
      }

      StringEncrypter myEncrypter;
      }
      ===================================================================================
      CF code:
      ===================================================================================
      <cfobject type="Java" action="create" class="com.adeptengr.EncryptDecrypt.StringEncryptWrapper" name="encrypter">

      <cfset encrypter.init()>
      <cfset encrypted = encrypter.encrypt("adepttest")>
      ===================================================================================
        • 1. Re: Java Object Instantiation
          -==cfSearching==- Level 4
          You are instantiating the class correctly. It is a problem with access modifiers. I do not know if you are familiar with java modifiers, but cffunction has similar a concept
          http://livedocs.adobe.com/coldfusion/7/htmldocs/00001063.htm


          Though the class itself is declared as "public"

          public class StringEncryptWrapper {
          ...
          }

          Your constructor is not.

          StringEncryptWrapper() {
          ...
          }


          I _think_ it is being assigned a default level of "package". Meaning you cannot instantiate it from outside the package: com.adeptengr.EncryptDecrypt. That is why you are receiving the "Unable to find constructor.." exeception. It is not that CF cannot find the constructor, it just is not allowed to access it.

          The solution is to explicitly declare the constructor _public_

          public StringEncryptWrapper() {
          ...
          }

          • 2. Re: Java Object Instantiation
            seyi103 Level 1
            Thanks a bunch, worked like a charm after declaring the constructor as public.