Java代码适用于eclipse,但不适用于命令提示符

时间:2014-05-13 19:42:19

标签: java eclipse

我正在编写一个java程序,它在Eclipse上正常运行,没有错误,程序提供准确的输出。但是,当我使用命令提示符编译它时,它给了我39个错误。

import java.util.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.*;

public class Main extends JFrame{

    public static void main (String args [])

    {

    JFrame frame = new JFrame (); // making a frame in which we will add all the
                                    // components
    FrameWork work = new FrameWork ();
    frame.setLayout(null);
    frame.setBounds (10,10,700,500);
    frame.setResizable(false); // making the frame non resizeable so that components are not misplaced
    frame.add (work);
    frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
    frame.setVisible (true);


    } // main ends here

} // main class ends here 



class FrameWork extends JPanel 
{
 // panel in which all the components are added 
    JPanel panel = new JPanel ();

    JTextArea jarea = new JTextArea();
    JTextField jfield = new JTextField();
    // Buttons that can be used to switch between standard IO NIO and NIO2
    JButton IO   = new JButton ("Standard IO");
    JButton NIO  = new JButton ("New IO");
    JButton NIO2 = new JButton ("New IO2");
    JScrollPane scroll = new JScrollPane();

FrameWork ()

{



// setting the lay out null so that componenets can be places at respective postions 
add(panel);
setLayout (null);
setBounds (0,0,700,500);
setBackground(Color.white);

add(jarea);
jarea.setBounds(5,85,670,360);
//setting border arround the JText area 
jarea.setBorder(BorderFactory.createLineBorder(Color.black));
jarea.add(scroll);


add(jfield);
jfield.setBounds(10,10,650,25);


 // adding Standard IO button and Implemnting action listener
add(IO);
IO.setBounds (50,40,150,40);
IO.addMouseListener(new MouseListener(){

    @Override
    public void mouseClicked(MouseEvent ae) 

    {       
        try {
            FileReader reader = new FileReader("test.txt");
            BufferedReader buff = new BufferedReader(reader);
            jarea.read(buff,null);
            buff.close();

        } 

        catch (IOException e) {


        }
    }

    @Override
    public void mouseEntered(MouseEvent ae) {


    }

    @Override
    public void mouseExited(MouseEvent ae) {


    }

    @Override
    public void mousePressed(MouseEvent ae) {


    }

    @Override
    public void mouseReleased(MouseEvent ae) {


    }});



// adding Standard NIO button and Implementing action listener


add(NIO);
NIO.setBounds (250,40,150,40);
NIO.addMouseListener(new MouseListener(){

    @Override
    public void mouseClicked(MouseEvent ae) {
// some part of this code from java docs

        Path file = Paths.get("test.txt");
        try (InputStream in = Files.newInputStream(file);
            BufferedReader reader =new BufferedReader(new InputStreamReader(in))) 

        {
            String line = null;
            while ((line = reader.readLine()) != null) {
                jarea.read(reader,null);
            }

        } catch (IOException x) {
            System.err.println(x);
        }




    }

    @Override
    public void mouseEntered(MouseEvent ae) {


    }

    @Override
    public void mouseExited(MouseEvent ae) {


    }

    @Override
    public void mousePressed(MouseEvent ae) {


    }

    @Override
    public void mouseReleased(MouseEvent ae) {


    }});

// adding Standard NIO2 button and Implemnting action listener


add(NIO2);
NIO2.setBounds (450,40,150,40);
NIO2.addMouseListener(new MouseListener(){

    @Override
    public void mouseClicked(MouseEvent ae) {
        try
        {
        ByteBuffer buffer = ByteBuffer.allocate(1024*1024);             
        Path file = Paths.get("test.txt");                          
        ReadableByteChannel rbc = Files.newByteChannel(file);               
        int counter =0;
        int flag = 0;
        int enter = 0;
        while(counter != -1)
        {
                buffer.rewind ();                                               
                counter = rbc.read(buffer);                                         
                buffer.rewind();
                flag++;
                for(enter =0 ; enter <= counter-1 ; enter++)
                {
                    byte by = buffer.get();                                 
                    jarea.append(""+(char)by);                              
                }
        }

        }
        catch(Exception e){}


    }

    @Override
    public void mouseEntered(MouseEvent ae) {


    }

    @Override
    public void mouseExited(MouseEvent ae) {


    }

    @Override
    public void mousePressed(MouseEvent ae) {


    }

    @Override
    public void mouseReleased(MouseEvent ae) {

    }});


}   // constructor ends 



}   //FrameWork Class ends

错误:

enter image description here

3 个答案:

答案 0 :(得分:1)

编辑:根据您稍后上传的错误图像,看起来您使用的是Java 1.6或更早版本的编译器。做java -version以查看您正在使用的版本。

更新您的Windows路径以使用您计算机上其他位置的java 1.7版本。

答案 1 :(得分:0)

在命令行中 - 当它是单个文件和独立应用程序时,删除顶部的导入包。

核对清单: -

让用户libraries可用于现有位置。

完全

Set the class path

执行以下命令以确保为jdk和jre

正确设置环境变量
c:\javac 
c:\java

答案 2 :(得分:0)

我今天遇到了类似的问题。我的程序在Eclipse调试器中运行良好,但在OS命令行上却行为异常。最终证明是CLASSPATH问题。我的代码与OS Shell的CLASSPATH环境变量中包含的JAR不兼容。我的程序甚至不需要此JAR。对于Eclipse,我仅链接了必需的JAR,因此该问题未在Eclipse调试器中显现。从我的CLASSPATH中排除有问题的JAR之后,问题消失了。