当前位置: 代码迷 >> Eclipse >> 为啥这段代码总是报未定义哇,跪求各位大侠解释,该如何处理
  详细解决方案

为啥这段代码总是报未定义哇,跪求各位大侠解释,该如何处理

热度:66   发布时间:2016-04-23 13:27:06.0
为啥这段代码总是报未定义哇,跪求各位大侠解释
import java.net.PasswordAuthentication;

public class MyAuthenticator {

String userName = null;
String password = null;

public MyAuthenticator()
{

}
public MyAuthenticator(String username,String pwd)
{
this.userName = username;
this.password = pwd;
}

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}

}

求教各位大侠,为啥

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}

这一段代码总是报错,The constructor PasswordAuthentication(String, String) is undefined

------解决方案--------------------
jdk中,PasswordAuthentication类的构造方法是:
PasswordAuthentication(String userName, char[] password) 
而不是
PasswordAuthentication(String userName, String password)
------解决方案--------------------
可以先对password调用toCharArray(),转化为char[]类型
  相关解决方案