0

I've got a project in Java (developped with Eclipse), in which I use some Swing components. At the start, I worked with JFrame : I created a new class herited from JPanel (PanelTr), added in it my JLabels, JTextField and JButton with an ActionListener on my button, and added an object PanelTr to my JFrame-herited class. Here's a piece of my code :

package gui;

public class PanelTr extends JPanel implements ActionListener {

    // Here I instantiate my JTextField and JButton used by ActionListener
    private JTextField textCap = new JTextField(20);
    private JButton submitButton = new JButton("Submit"); 

    public PanelTr() {

        // Here I add my JLabels and my layout manager

        submitButton.addActionListener(this);


}

    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource()==submitButton)
        {
            JOptionPane.showMessageDialog(null,"DEBUG"); // Just to see if it is displayed
            String err = Analyzer.process(textCap.getText().toString()); // An analyzing process I use in my program
            if (err=="")
            {
                JOptionPane.showMessageDialog(null,"OK");
            }
            else
            {
                JOptionPane.showMessageDialog(null,err,"Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    }   
}

And my JFrame :

package gui;

public class FrameTr extends JFrame
{
    public FrameTr()
    {
        PanelFils ctn = new PanelFils(); //Conteneur
        setContentPane(ctn);
        ctn.setBackground(new Color(0,255,255));
        setTitle("Project");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[])
    {
        new FrameTr();
    }
}

In my Analyzer class, I use several Excel files located in the root folder of my project.

So with this code, my program runs well, on Eclipse and when I export it into a runnable jar (I place my Excel files in the same folder of my .jar file).

But now I need to make an Applet which allows the program to run on a web browser. So I created a new JApplet-based class with just this code :

package gui;

public class TestApplet extends JApplet{

    public void init()
    {
        PanelTr ctn = new PanelTr();
        setContentPane(ctn);
    }
}

When I run this class on Eclipse, my debug JOptionPane is displayed, but in the analyzing process I get a FileNotFoundException on my first Excel File. I tried to move it to the src folder, to the gui folder, even to the analyse folder (package of my Analyzer class), but still this FileNotFoundException...

I also tried to run my program in a webpage, so I exported as a runnable jar (Note : I couldn't choose TestApplet in my launch configuration since it doesn't have a main(), so I choosed FrameTr). I used the tag :

<applet archive="app.jar" width="700" height="100" code="gui/TestApplet.class"/>

My pane is displayed, but when I click on my button, the debug JOptionPane doesn't pop out !

So first, how can I solve the FileNotFound problem ? And then, why my JOptionPane isn't displayed on my web browser ?

5
  • How are you trying to load the file?
    – tddmonkey
    Commented Apr 24, 2015 at 9:34
  • Applets run in the sandbox and have limited access to your computer. Are you sure it can access the files?
    – Tim B
    Commented Apr 24, 2015 at 9:36
  • @MrWiggles I load the file with : FileInputStream file = new FileInputStream(new File("Countries.xlsx")); then I treat it with Apache POI, which works well with JFrame. :)
    – Malik
    Commented Apr 24, 2015 at 9:37
  • @TimB Well on eclipse I left the file in the root folder, and on my web server, the file is in the same folder than the jar.
    – Malik
    Commented Apr 24, 2015 at 9:39
  • Be sure the Java Console is configured to show. If there is no output at the default level, raise the level and try it again. Commented Apr 24, 2015 at 9:58

2 Answers 2

2

Applets are restricted and can't access File System (with exception for signed applets).

If the files inside your jar you can use them as resource. Instead of using e.g. FileInputStream use this.getClass().getResourceAsStream("/Countries.xlsx") (the Countries.xlsx should be in the root)

4
  • I can't use "this", because I don't instantiate any object from Analyzer (no constructor) and all my methods are static, is there any other way ?
    – Malik
    Commented Apr 24, 2015 at 9:45
  • TestApplet.class.getResourceAsStream("/Countries.xlsx")
    – StanislavL
    Commented Apr 24, 2015 at 9:49
  • It doesn't work in my web browser. I used the following line to replace my old "FileInputStream..." : InputStream file = TestApplet.class.getResourceAsStream("/Countries.xlsx"); and no result... How can I test it in Eclipse ?
    – Malik
    Commented Apr 24, 2015 at 10:06
  • Check where the xlxs file is located? Does it exists in the same dir/package where your classes are located?
    – StanislavL
    Commented Apr 24, 2015 at 10:51
1

The Java Applet is executed client side - it runs on the computer of the person using the website. It does not run on the web server.

If you have files in the server then the client will not be able to access them unless you serve them up from your web server and access them via HTTP(S).

If the value in the files are constants then you should put the files inside your JAR and distribute them as part of the Applet.

6
  • Oh, I see, so what I'm doing is absolutely useless then ! Actually, I've got some static resources from 5 Excel files, and also 1 Excel file for my results. Every time I give a URL to my program, it analyzes it through my 5 static files then add a line to my result excel file. So actually now the results are made "locally" and not in the server, so it's pointless ! Am I right ?
    – Malik
    Commented Apr 24, 2015 at 10:10
  • Yes, that's pretty much it.
    – Tim B
    Commented Apr 24, 2015 at 10:11
  • Wow, thank you for making me realize that, I've lost some time doing this but at least I've learnt something ! So if I understanded, I have to connect from the applet to my Web Server (via HTTP/HTTPS) in order to get my files... The big problem in that solution is getting files from the server, then pushing into the server, since I work in a big society which has an awful lot of firewalls, proxies etc. So I think I'll have to find another solution ! Can JavaEE be useful in my case then ? (Since it's executed in the server)
    – Malik
    Commented Apr 24, 2015 at 10:23
  • @Malik Probably. It's too big a question for comments though (probably too broad even asked as a question here). You need to rethink your architecture, that much is certain.
    – Tim B
    Commented Apr 24, 2015 at 10:28
  • "So if I understanded, I have to connect from the applet to my Web Server (via HTTP/HTTPS) in order to get my files..." Not (exactly) necessarily. It is possible to include the files in the applet Jar and access them as an embedded-resource. Commented Apr 25, 2015 at 5:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.