主题:[讨论]java编译成功,运行出错,怎么办?
写按教程上的例子抄了一下,编译没有出错,但运行时报以下的错,请问怎么解决:
D:\java>java client
Exception in thread "main" java.lang.NoClassDefFoundError: client (wrong name: C
lient)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(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)
Could not find the main class: client. Program will exit.
Client.java
public class Client
{
public static void main (String[] args)
{
AppConfig config = new AppConfig();
String paramA = config.getParameterA();
String paramB = config.getParameterB();
System.out.println("paramA="+paramA+",paramB="+paramB);
}
}
AppConfig.java
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
//读取应用配置文件
public class AppConfig
{
//用来存放配置文件中参数A的值
private String parameterA;
//用来存放配置文件中参数B的值
private String parameterB;
public String getParameterA()
{
return parameterA;
}
public String getParameterB()
{
return parameterB;
}
//构造方法
public AppConfig()
{
readConfig();
}
//读取配置文件,把配置文件中的内容读出来设置到属性上
private void readConfig()
{
Properties p = new Properties();
InputStream in = null;
try
{
in = AppConfig.class.getResourceAsStream("Appconfig.properties");
p.load(in);
//把配置文件中的内容读出来设置到属性上
this.parameterA = p.getProperty("paramA");
this.parameterB = p.getProperty("paramB");
}
catch (IOException e)
{
System.out.println("装载配置文件出错了,具体堆栈信息如下:");
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
D:\java>java client
Exception in thread "main" java.lang.NoClassDefFoundError: client (wrong name: C
lient)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(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)
Could not find the main class: client. Program will exit.
Client.java
public class Client
{
public static void main (String[] args)
{
AppConfig config = new AppConfig();
String paramA = config.getParameterA();
String paramB = config.getParameterB();
System.out.println("paramA="+paramA+",paramB="+paramB);
}
}
AppConfig.java
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
//读取应用配置文件
public class AppConfig
{
//用来存放配置文件中参数A的值
private String parameterA;
//用来存放配置文件中参数B的值
private String parameterB;
public String getParameterA()
{
return parameterA;
}
public String getParameterB()
{
return parameterB;
}
//构造方法
public AppConfig()
{
readConfig();
}
//读取配置文件,把配置文件中的内容读出来设置到属性上
private void readConfig()
{
Properties p = new Properties();
InputStream in = null;
try
{
in = AppConfig.class.getResourceAsStream("Appconfig.properties");
p.load(in);
//把配置文件中的内容读出来设置到属性上
this.parameterA = p.getProperty("paramA");
this.parameterB = p.getProperty("paramB");
}
catch (IOException e)
{
System.out.println("装载配置文件出错了,具体堆栈信息如下:");
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}