1

I have an applet class and I want that when I click the button on the applet, then it runs Java Class.

It runs Java Class, but in the Java Class I have created a .csv file, so it gives me error of

file not found

..... If I separately run the Java Class it gives the correct output ....

What's the problem in linking the applet and Java Class?

Applet class

import java.applet.*;  
import java.awt.*;  
import java.awt.event.*;  
public class anApplet extends Applet implements ActionListener{  
Button b;  
TextField tf;  

public void init(){  
tf=new TextField();  
tf.setBounds(30,40,150,20);  

b=new Button("Click");  
b.setBounds(80,150,60,50);  

add(b);add(tf);  
b.addActionListener(this);  

setLayout(null);  
}  

 public void actionPerformed(ActionEvent e){  
 tf.setText("Welcome"); 
try {
callingClass.main(null);
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}   
}  

Java Class

 public class callingClass {
 public static void main(String[] main) throws Exception, Throwable
 {
      String outputFile = "Data\\Volume.csv";

    // before we open the file check to see if it already exists
    boolean alreadyExists = new File(outputFile).exists();
    try {
        // use FileWriter constructor that specifies open for appending
        CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');

        // if the file didn't already exist then we need to write out the header line
        if (!alreadyExists)
        {
            csvOutput.write("date");
            csvOutput.write("value");
            csvOutput.endRecord();
        }
        // else assume that the file already has the correct header line

        // write out a few records
        csvOutput.write("Hello");
        csvOutput.write( "CSV file");
        csvOutput.endRecord();

        //csvOutput.write("2");
        //csvOutput.write("John");
    //  csvOutput.endRecord();

        csvOutput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) An Applet can only write files on the client PC (and then only if the code is signed and requests `all-permissions`). That is probably the problem here. – Andrew Thompson Apr 11 '15 at 10:23

0 Answers0