写了一个下载器,作用是根据URL地址下载一个网页文件。这里是运行后的报错
使用的URL打开网络文件并写入到一个文件中去
输入的指令为java Downloader http://www.javasoft.com/index.html C:/brett/temp/javahome.html  下面有可能不方便看,这里单列一下Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.C:\Documents and Settings\Iuyasha>cd C:\Documents and Settings\Iuyasha\My Docume
nts\Code\Chapter03C:\Documents and Settings\Iuyasha\My Documents\Code\Chapter03>java Downloader http://www.javasoft.com/index.html C:/brett/temp/javahome.html
Exception in thread "main" java.lang.NoClassDefFoundError: Downloader
Caused by: java.lang.ClassNotFoundException: Downloader
        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.ClassLoader.loadClassInternal(Unknown Source)C:\Documents and Settings\Iuyasha\My Documents\Code\Chapter03>这个感觉不像是代码的问题,是配置的问题?但是现在 runtime和jdk的版本都是最新的了
源码在这里,与URL下载有关的部分我标出来了
类Downloader的构造方法
public Downloader(URL eUrl, OutputStream os)throws IOException{

downloadURL=eUrl;
outputStream=os;
bytesRead=0;

URLConnection urlConnection=downloadURL.openConnection();
filesize=urlConnection.getContentLength();
if(filesize==-1){
throw new FileNotFoundException(eUrl.toString());
}

inputStream=new BufferedInputStream(urlConnection.getInputStream());
buffer=new byte[BUFFER_SIZE];

thisThread=new Thread(this);

buildLayout();

stopped=false;
}
类Downloader的主方法
public static void main(String[] args) throws Exception{
Downloader dl=null;

if(args.length<2){
System.out.println("Incorrect downloading infomation");
System.exit(0);
}


URL url=new URL(args[0]);

FileOutputStream fos=new FileOutputStream(args[1]);

try{
dl=new Downloader(url,fos);
}catch(FileNotFoundException fnfe){
System.out.println("File does not exist");
System.exit(0);
}

JFrame jf=new JFrame();
jf.getContentPane().add(dl);
jf.setSize(600,200);
jf.setVisible(true);

//dl.thisThread.start();
}
其他都是些异常捕捉和GUI设计的部分,应该没什么影响。另有完整代码如下
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;public class Downloader extends JPanel implements Runnable{

protected URL downloadURL;
protected InputStream inputStream;
protected OutputStream outputStream;
protected byte[] buffer;

protected int filesize;
protected int bytesRead;

protected JLabel urlLabel;
protected JLabel sizeLabel;
protected JLabel completedLabel;
protected JProgressBar progressBar;

public final static int BUFFER_SIZE=1024;

protected boolean stopped;
protected Thread thisThread;

public static void main(String[] args) throws Exception{
Downloader dl=null;

if(args.length<2){
System.out.println("Incorrect downloading infomation");
System.exit(0);
}


URL url=new URL(args[0]);

FileOutputStream fos=new FileOutputStream(args[1]);

try{
dl=new Downloader(url,fos);
}catch(FileNotFoundException fnfe){
System.out.println("File does not exist");
System.exit(0);
}

JFrame jf=new JFrame();
jf.getContentPane().add(dl);
jf.setSize(600,200);
jf.setVisible(true);

//dl.thisThread.start();
}

public Downloader(URL eUrl, OutputStream os)throws IOException{

downloadURL=eUrl;
outputStream=os;
bytesRead=0;

URLConnection urlConnection=downloadURL.openConnection();
filesize=urlConnection.getContentLength();
if(filesize==-1){
throw new FileNotFoundException(eUrl.toString());
}

inputStream=new BufferedInputStream(urlConnection.getInputStream());
buffer=new byte[BUFFER_SIZE];

thisThread=new Thread(this);

buildLayout();

stopped=false;
}

protected void buildLayout(){
JLabel label;
setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(5,10,5,10);

gbc.gridx=0;
label=new JLabel("URL",JLabel.LEFT);
add(label,gbc);

label=new JLabel("Completed",JLabel.LEFT);
add(label,gbc);

label=new JLabel("Downloaded",JLabel.LEFT);
add(label,gbc);

gbc.gridx=1;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.weightx=1;

urlLabel=new JLabel(downloadURL.toString());
add(urlLabel,gbc);

progressBar=new JProgressBar(0,filesize);
progressBar.setStringPainted(true);
add(progressBar,gbc);

gbc.gridwidth=1;
completedLabel=new JLabel(Integer.toString(bytesRead));
add(completedLabel,gbc);

gbc.gridx=2;
gbc.weightx=0;
gbc.anchor=GridBagConstraints.EAST;
label=new JLabel("Size",JLabel.LEFT);
add(label,gbc);

gbc.gridx=3;
gbc.weightx=1;
sizeLabel= new JLabel(Integer.toString(filesize));
add(sizeLabel,gbc);
}

public void run(){
performedDownload();
}

protected void performedDownload(){
int byteCount;
while((bytesRead<filesize)&&(!stopped)){
try{
byteCount=inputStream.read(buffer);
if(byteCount==-1){
stopped=true;
break;
}else{
outputStream.write(buffer,0,byteCount);
bytesRead+=byteCount;
progressBar.setValue(bytesRead);
completedLabel.setText(Integer.toString(bytesRead));
}
}catch(IOException ioe){
stopped=true;
JOptionPane.showMessageDialog(this,ioe.getMessage(),"IOError",JOptionPane.ERROR_MESSAGE);
break;
}
try{
outputStream.close();
inputStream.close();
}catch(IOException ioe){
};
}
}
}

解决方案 »

  1.   

    楼主先执行一下javac Downloader然后在执行一下java Downloader http://www.javasoft.com/index.html C:/brett/temp/javahome.html 试试看
      

  2.   

    Exception in thread "main" java.lang.NoClassDefFoundError: Downloader找不到Downloader呀
      

  3.   

    编译了没有?
    javac Downloader.java
    java Downloader http://www.javasoft.com/index.html C:/brett/temp/javahome.html
      

  4.   

    Exception in thread "main" java.lang.NoClassDefFoundError: Downloader 
    Caused by: java.lang.ClassNotFoundException: Downloader 
    不是说的很清楚吗?没有找到Downloader这个类