Java 未知异常 求解
package javaapplication2;import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaApplication2
{
public static void main(String args[])
{
new C().aa();
}
}
class Con
{
static public Socket s = null;
static private OutputStream op = null;
public static void send(String str)
{
try
{
s = new Socket("192.168.1.2", 12345);
op = s.getOutputStream();
op.write(str.getBytes());
} catch (IOException e) {
System.out.println(e);
}
finally
{
try {
op.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
}
class C extends Frame
{
privatefinal TextField tf = new TextField(50);;
public void aa()
{
setTitle("聊天");
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
// tf= new TextField(50);
p.add(tf);
Button b = new Button("Go");
b.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
Con.send(tf.getText());
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e) {
try {
Con.s.close();
super.windowClosing(e); //To change body of generated methods, choose Tools | Templates.
} catch (IOException ex) {
Logger.getLogger(C.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
System.exit(1);
}
}
});
p.add(b);
add(p, BorderLayout.SOUTH);
TextArea ta = new TextArea("",50,50,TextArea.SCROLLBARS_VERTICAL_ONLY );
add(ta,BorderLayout.NORTH);
pack();
setVisible(true);
}
/* class action implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
new Con().send(tf.getText());
}
}*/
}
run:
Exception in thread "main" java.lang.NoClassDefFoundError: javaapplication2/C
at javaapplication2.JavaApplication2.main(JavaApplication2.java:14)
Caused by: java.lang.ClassNotFoundException: javaapplication2.C
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Java Result: 1
成功构建 (总时间: 0 秒)
环境:NetBeans
eclipse,cmd下都可编译通过并运行
把con类相关内容删掉,也可正常运行
求解 也许只是NetBeans的问题吧? 花2分钟研究了一下你的代码,我的eclipse直接报了莫名其妙的错误
Con改成MyCon,没有报错,但是再改回去,提示错误'Con' isan invalid name on this platform
去百度,得到
con是DOS保留字,好象是一设备,WINDOWS也是建立在DOS基础上的。
所以,不能再windows下面创建名字为con/Con,CON 的类!哈哈哈!我都忘了这个问题了,还有一些其他的类名也不能建立
con是操作系统保留的一个设备名字,还有很多设备名都不能拿来用,如下:
The following reserved device names cannot be used as the name of a file: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed by an extension (for example, NUL.tx7).
可见与NetBean神马的,没啥关系,名字冲突了而已 con是键盘输入。用DOS命令创建文件的方式是
copy con foo.txt
然后输入内容,最后按Ctrl+Z结束。
con相当于stdin
页:
[1]