当前位置: 技术问答>java相关
import的奇怪问题!!高分送上!!
来源: 互联网 发布时间:2015-08-08
本文导语: import java.io.*; import java.util.*; public class getCLI { static String localhost; static String postmaster; static int port; static String pass; static String accountpath; File manager = new File("c:\cgp\properties.props"); public boolean getConnect(){ ...
import java.io.*;
import java.util.*;
public class getCLI
{
static String localhost;
static String postmaster;
static int port;
static String pass;
static String accountpath;
File manager = new File("c:\cgp\properties.props");
public boolean getConnect(){
if(!manager.exists()){
return false;
}else{
return true;
}
}
public void getCLI() throws Exception
{
String port_temp;
FileInputStream fin = new FileInputStream(manager);
Properties props = new Properties();
props.load(fin);
this.localhost = (String)props.getProperty("localhost");
this.postmaster = (String)props.getProperty("postmaster");
port_temp = (String)props.getProperty("port");
this.port = new Integer(Integer.parseInt(port_temp)).intValue();
this.pass = (String)props.getProperty("pass");
this.accountpath = (String)props.get("DomainsPath");
fin.close();
}
public String getLocalhost(){
return this.localhost;
}
public String getPostmaster(){
return this.postmaster;
}
public int getPort(){
return this.port;
}
public String getPass(){
return this.pass;
}
public String getAccountpath(){
return this.accountpath;
}
}
成功编译,我在另外一个java程序中import getCLI;目录正确都在当前目录,在这个java程序中我new一个getCLI对象:getCLI getcli = new getCLI();
之后这样使用:
this.getcli = new getCLI();
try{
if(this.getcli.getConnect()){
localhost = this.getcli.getLocalhost();
postmaster = this.getcli.getPostmaster();
port = this.getcli.getPort();
pass = this.getcli.getPass();
}else{
try{
out.println("
cli连接错误,请检查配置文件
");import java.util.*;
public class getCLI
{
static String localhost;
static String postmaster;
static int port;
static String pass;
static String accountpath;
File manager = new File("c:\cgp\properties.props");
public boolean getConnect(){
if(!manager.exists()){
return false;
}else{
return true;
}
}
public void getCLI() throws Exception
{
String port_temp;
FileInputStream fin = new FileInputStream(manager);
Properties props = new Properties();
props.load(fin);
this.localhost = (String)props.getProperty("localhost");
this.postmaster = (String)props.getProperty("postmaster");
port_temp = (String)props.getProperty("port");
this.port = new Integer(Integer.parseInt(port_temp)).intValue();
this.pass = (String)props.getProperty("pass");
this.accountpath = (String)props.get("DomainsPath");
fin.close();
}
public String getLocalhost(){
return this.localhost;
}
public String getPostmaster(){
return this.postmaster;
}
public int getPort(){
return this.port;
}
public String getPass(){
return this.pass;
}
public String getAccountpath(){
return this.accountpath;
}
}
成功编译,我在另外一个java程序中import getCLI;目录正确都在当前目录,在这个java程序中我new一个getCLI对象:getCLI getcli = new getCLI();
之后这样使用:
this.getcli = new getCLI();
try{
if(this.getcli.getConnect()){
localhost = this.getcli.getLocalhost();
postmaster = this.getcli.getPostmaster();
port = this.getcli.getPort();
pass = this.getcli.getPass();
}else{
try{
out.println("
cli连接错误,请检查配置文件
}catch(IOException ioe){}
}
}catch(Exception e){
System.out.println("1111");
}
这样可以正常编译。
但是却在运行时抛出异常:
java.lang.NoClassDefFoundError: getCLI
help!!!!
|
实在不行就把两个文件都放到package里头,再import。
|
import是引用包,你将引用的类加入到一个包里就可以了。
|
将getCLI放在一个包里,此包的目录放到CLASSPATH的任一根目录下,
在另一个文件里 IMPORT 包名.getCLI;
实在不行的话,把两个类放到同一个文件里,肯定OK!! :)
在另一个文件里 IMPORT 包名.getCLI;
实在不行的话,把两个类放到同一个文件里,肯定OK!! :)
|
另外一个java程序中and getCLI
都放在同一个目录下面
不用import
都放在同一个目录下面
不用import