Issue
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:
Solution
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.
Answered By - Alcanzar