Forum Stats

  • 3,874,310 Users
  • 2,266,718 Discussions
  • 7,911,811 Comments

Discussions

JXL -- Applet -- NoClassDefFoundError

843810
843810 Member Posts: 46,938
First off, I have no clue if this is the correct place to post this, and I'm sorry if it isnt but I couldnt find a forum more specific.

I am building a project that will be an applet and ran from my school's web server. The program will work similarly to how freerice.com does their thing, only it will include latin vocabulary and there is no free rice for the poor.

In order to hold all of the vocab words/user data I have decided to use an excel sheet. This is because the data has already been created and it is in an excel sheet, so its just easier that way. So since I will be using an excel sheet for my databasing, I found jxl to be a potentially useful tool.

I ran a test/learning program to figure out how jxl works, the possible issues I would run into, ect. THAT program was an application. So after getting to a place where I feel comfortable with jxl, I decided to make the necessary changes to my applet to include jxl. However I had some issues which I will talk about below. Here is the code and output for each program.

Test/Learning Application Code:
//Imports
import java.io.File;
import java.util.Date;
import jxl.*;
import jxl.write.*;
import java.io.*;

public class Excel 
{
    //Main
    public static void main(String[] args) throws Exception
    {
    	//Read
    	//Get workbook
    	Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
    	//Get sheet
    	Sheet sheet = workbook.getSheet(0);
    	//Get Cells
    	Cell a1 = sheet.getCell(0,0);
		Cell b2 = sheet.getCell(1,1);
		Cell c2 = sheet.getCell(2,1);
		//Get cell content
		String stringa1 = a1.getContents();
		String stringb2 = b2.getContents();
		String stringc2 = c2.getContents(); 
		//Print out cell content
		System.out.println(stringa1);
		System.out.println(stringb2);
		System.out.println(stringc2);
		//Close workbook
    	workbook.close(); 
    	
    	//Write
    	//Get workbook
    	Workbook workbook2 = Workbook.getWorkbook(new File("myfile.xls"));
    	//Create copy
    	WritableWorkbook copy = Workbook.createWorkbook(new File("myfile.xls"), workbook2);
    	//Get sheet to be written to
    	WritableSheet sheet2 = copy.getSheet(0);
    	//Get cell to be written to
		WritableCell cell = sheet2.getWritableCell(1, 2);
		//Write
		if (cell.getType() == CellType.LABEL)
		{
		  	Label l = (Label) cell;
		  	l.setString("modified cell");
		} 
		//Write and Close workbook
		copy.write();
		copy.close(); 
		
		//Read again to check for changes
		workbook = Workbook.getWorkbook(new File("myfile.xls"));
    	sheet = workbook.getSheet(0);
    	a1 = sheet.getCell(0,0);
		b2 = sheet.getCell(1,1);
		c2 = sheet.getCell(2,1);
		stringa1 = a1.getContents();
		stringb2 = b2.getContents();
		stringc2 = c2.getContents(); 	
		System.out.println(stringa1);
		System.out.println(stringb2);
		System.out.println(stringc2);
    	workbook.close(); 
    }
}
Output:
(0,0)
(1,1)
(2,1)
(0,0)
(1,1)
(2,1)
That is exactly what I wanted to get, so there are no issues here.

Applet Code (edited for space and importance):
//Imports
//...
import jxl.*;
import jxl.write.*;
import java.io.File;
import java.io.*;

public class Host 
{
        //...
	ArrayList<User> userArr = new ArrayList<User>();
	
	JApplet app;
	
    public Host(JApplet j) 
    {
    	//...
    	app = j;
    	readUsers();
    	for(int i=0;i<userArr.size();i++)
    	{
    		User user = userArr.get(i);
    		System.out.println(user.getName()+" "+user.getPassword()+" "+user.getLevel()+" "+user.getPoints());
    	}
    }
    
    //Method having trouble
    public void readUsers()
    {
    	try
    	{
                //Runtime Error Occurring here
    		Workbook workbook = Workbook.getWorkbook(new File("data.xls"));
	    	Sheet sheet = workbook.getSheet(1);
	    	for(int i=0;i<sheet.getRows();i++)
	    	{
	    		Cell nameC = sheet.getCell(i,0);
	    		Cell passC = sheet.getCell(i,1);
	    		Cell lvlC = sheet.getCell(i,2);
	    		Cell ptsC = sheet.getCell(i,3);
	    		String name = nameC.getContents();
	    		String password = passC.getContents();
	    		int level = Integer.parseInt(lvlC.getContents());
	    		int points = Integer.parseInt(ptsC.getContents());
	    		userArr.add(new User(name,password,level,points));
	    	}
	    	workbook.close(); 
    	}
    	catch(Exception e)
    	{
    		System.out.println(e);
    	}
    }
}
Output (RUNTIME error):
java.lang.NoClassDefFoundError: jxl/Workbook
    at Host.readUsers(Host.java:47)
    at Host.<init>(Host.java:35)
    at Vocab.init(Vocab.java:26)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: jxl.Workbook
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:210)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:143)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 5 more
So I'm not sure why this is happening here. The only significant difference between the two projects is that one is an application and the other is an applet.

I understand what the error is saying, but I cant seem to find out how to fix it. I just wanted to provide as much information as possible in hope for better solutions. Any help is greatly appreciated.

Comments

  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    Almost always means a class path problem.

    An applet exists in a server and is delivered to a client where it runs. It doesn't run on the server. When the applet runs everything it needs must be on the client.

    That includes your third party library and probably the excel file as well.

    I believe you would normally do the following.
    - Embed the third party library as a jar in your applet jar. You must manually modify the jar manifest for this to work.
    - Embed the excel file also and access it as a resource.

    There are other possible solutions.

    Above is a guess since I don't do applets (nor client anything) but asking on a GUI forum would probably get better responses. If you re-post make sure you mention that it is a repost. Or ask the mods to move the thread via the admin forum.
  • 843810
    843810 Member Posts: 46,938
    jschell wrote:
    Almost always means a class path problem.

    An applet exists in a server and is delivered to a client where it runs. It doesn't run on the server. When the applet runs everything it needs must be on the client.

    That includes your third party library and probably the excel file as well.

    I believe you would normally do the following.
    - Embed the third party library as a jar in your applet jar. You must manually modify the jar manifest for this to work.
    - Embed the excel file also and access it as a resource.

    There are other possible solutions.

    Above is a guess since I don't do applets (nor client anything) but asking on a GUI forum would probably get better responses. If you re-post make sure you mention that it is a repost. Or ask the mods to move the thread via the admin forum.
    Ok, well just for clarification, it is possible to have the library and excel file on the server correct? Because for my program to work, I absolutely MUST have the excel file on the server and available for all users to access. This is because the excel file will be both written to and read from by each user.
    As for the applet jar, I was hoping to avoid creating a jar for it and just embed the main class directly into the browser (done that several times before).
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    Aussiemcgr wrote:
    Ok, well just for clarification, it is possible to have the library and excel file on the server correct?
    I seriously doubt it. I would expect that the library you are using requires access to a file. The applet runs on the client thus the file system that the library will use will be that of the client.

    So at some point the excel file will need to be on the client. Alternatives to this
    1. The library supports a stream rather than a file. Then you would need to write quite a bit of code to create a stream that loads the data across the network.
    2. The client OS has a mapped drive with the server. This is an OS feature not a java feature. It is also unsecure on the internet so only suitable, at best, on a private network.
    This is because the excel file will be both written to and read from by each user.
    I think you need to start over from the architecture and design point of view.

    Consider this what exactly happens in an excel file when two users try to write to it at the same time. This is NOT a java question.

    I already told you that the easiest way to do is to have a copy of the file on the client machine. Your requirement above completely rules this out. The only possible solution using only an applet requires a mapped drive. And that has the previously mentioned limitations. And even then you are going to need a design that deals with that.

    A better solution
    1. Use an actual java server, not just an applet.
    2. Move the data into a real database.
This discussion has been closed.