我正在构建的应用程序中有以下界面:
package sistemata;
public interface PaymentgatewayAdapter
{
public String postPayment(String a);
}
以下类实现它:
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;
//------------------------------
}
}
有一个名为Payson的第二个几乎相同的类,但没有使用它。
此界面是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;
}
}
方法getPaymentAdapter
在Account
类中调用,如下所示:
else if (paymentMethod == 0)
{
System.out.println("The payment method is PayPal INC");
String className = "PaypalAdapter";
paymentAdapter = ServicesFactory.getInstance().getPaymentAdapter(className);
}
每当我打电话:
paymentAdapter = ServicesFactory.getInstance().getPaymentAdapter(className);
我收到System.out.println("ERROR: Cannot instantiate:.
中引发的错误
无论我尝试什么,我无法解决这个问题,我无法找到解决方案。我将感谢您对此代码的一些帮助,甚至是硬编码的建议。
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)
答案 0 :(得分:4)
您是否忘记了此行中的包裹名称?
String className = "PaypalAdapter";
应该是
String className = "sistemata.x.y.z.PaypalAdapter";
答案 1 :(得分:2)
传递给Class.forName()
的类名必须是完全限定的类名,包括包。尝试使用“sistema.PaypalAdapter”。