Reputation: 1177
I have the following interface in the application I am building:
package sistemata;
public interface PaymentgatewayAdapter
{
public String postPayment(String a);
}
And the following class implements it:
public class PaypalAdapter implements PaymentgatewayAdapter
{
public String postPayment(String a)
{
String payAmount;
String transactionOutcome;
String response;
payAmount = a;
Paypal_sim systemGN = new Paypal_sim();
System.out.println("v adaptera" + a);
//Simulerar att data skickas till externa systemet
response = systemGN.transferSaleData(payAmount);
if (response == "OK")
{
transactionOutcome = "OK";
}
else
{
transactionOutcome = "Invalid data sent to MasterCard.";
}
return transactionOutcome;
//------------------------------
}
}
There is a second, almost identical class called Payson, but it is not used.
This interface is part of a ServicesFactory
:
package sistemata;
public class ServicesFactory
{
private static ServicesFactory instance;
private PaymentgatewayAdapter paymentAdapter;
private String className2;
public static synchronized ServicesFactory getInstance()
{
instance = new ServicesFactory();
return instance;
}
public PaymentgatewayAdapter getPaymentAdapter(String className2)
{
this.className2 = className2;
if (paymentAdapter == null)
{
try
{
paymentAdapter = (PaymentgatewayAdapter)
Class.forName(className2).newInstance();
}
catch (Exception e)
{
System.out.println("ERROR: Cannot instantiate: " +
className2 + ".");
e.printStackTrace();
System.exit(1);
}
}
return paymentAdapter;
}
}
The method getPaymentAdapter
is called in Account
class, like this:
else if (paymentMethod == 0)
{
System.out.println("The payment method is PayPal INC");
String className = "PaypalAdapter";
paymentAdapter = ServicesFactory.getInstance().getPaymentAdapter(className);
}
Whenever I call:
paymentAdapter = ServicesFactory.getInstance().getPaymentAdapter(className);
I get the error that is thrown in System.out.println("ERROR: Cannot instantiate:.
Whatever I try, I cannot work around this and I cannot find solution. I will be thankful for some help with this code, or even suggestions to hardcode.
java.lang.ClassNotFoundException: PaymentgatewayAdapter
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sistemata.ServicesFactory.getPaymentAdapter(ServicesFactory.java:26)
at sistemata.Account.makePayment(Account.java:56)
at sistemata.MainSystem.main(MainSystem.java:71)
Upvotes: 0
Views: 1013
Reputation: 12766
The class name you pass to Class.forName()
must be a fully-qualified class name, including the package. Try using "sistema.PaypalAdapter".
Upvotes: 2
Reputation: 40378
Did you forget the package name from this line?
String className = "PaypalAdapter";
should it be
String className = "sistemata.x.y.z.PaypalAdapter";
Upvotes: 4