1

I am writing a java program and it is properly running on Eclipse, there are no errors and the program gives accurate output. However when I compile it using command prompt it gives me 39 errors.

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

Errors:

enter image description here

6
  • 3
    What errors is it giving you precisely? Commented May 13, 2014 at 19:44
  • 2
    This is most likely because you do not have the corect jars in the classpath. Can you provide a couple of the errors? Commented May 13, 2014 at 19:47
  • @Nivas: and what JARs would those be? The imports only refer to standard Java SE classes. Commented May 13, 2014 at 19:51
  • Could you show us the command you used to start the application from command line? And show us the error message(s). Commented May 13, 2014 at 19:52
  • I have added the pictures of errors which I am getting while compiling the program. I am using javac command for compiling the code Commented May 13, 2014 at 20:01

3 Answers 3

1

Edit: based on the images of errors that you uploaded later, it looks like you are using a Java 1.6 or earlier compiler. Do java -version to see what version you are using.

Update your windows path to use the java 1.7 version that you have elsewhere on your machine.

Sign up to request clarification or add additional context in comments.

1 Comment

based on your image, it looks like your java compiler is older than java 1.7 and you are using try with resources which was introduced in 1.7
0

In Command Line - Remove the imported packages on top , when it is a single file and standalone application.

Check List:-

Make user the libraries are available to the existing location.

Set the class path perfectly.

Execute the below commands to ensure they environmental variables are set properly for jdk and jre

c:\javac 
c:\java

Comments

0

I ran into a similar issue today. My program was working fine in the Eclipse debugger but misbehaving from the OS command line. It finally turned out to be a CLASSPATH issue. My code was incompatible with a JAR that I had included in my CLASSPATH environment variable for the OS shell. This JAR was not even required for my program. For Eclipse I had only linked the requisite JARs, so this problem did not manifest in the Eclipse debugger. After excluding the offending JAR from my CLASSPATH, the problem disappeared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.