Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Java Object Instantiation

Explorer ,
Jun 19, 2008 Jun 19, 2008
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")>
===================================================================================
TOPICS
Advanced techniques
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Jun 19, 2008 Jun 19, 2008
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 ou...
Translate
Valorous Hero ,
Jun 19, 2008 Jun 19, 2008
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() {
...
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 19, 2008 Jun 19, 2008
LATEST
Thanks a bunch, worked like a charm after declaring the constructor as public.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources