Search code examples
javaeclipsejakarta-ee

Java class not found errors while running


I have a class that is having some dependency problems referencing external library files. Every time I try to run this on the server I get errors saying class not found, such as this:

SEVERE: Class [ org/json/JSONException ] not found. Error while loading [ class com.myproj.logic.Driver ]

this is preventing the class from executing. I tried taking out the specific throws execption by just saying "throws exception" and got the following error:

WARNING: A system exception occurred during an invocation on EJB Driver method public void com..logic.Driver.initURL() throws java.lang.Exception javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton Driver

Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils

@Singleton
public class Driver {
 
@EJB RSSbean rssbean;    
   
@PostConstruct
@Schedule(hour ="*/1", minute ="*/1", second ="*/1", persistent = false)
public void initURL() throws IOException, JSONException{
     
    URL twitterSource = new URL("http://search.twitter.com/search.json?q=news");
    ByteArrayOutputStream urlOutputStream = new ByteArrayOutputStream();

                IOUtils.copy(twitterSource.openStream(), urlOutputStream);
                String urlContents = urlOutputStream.toString();
                JSONObject thisobject = new JSONObject(urlContents);
                JSONArray names = thisobject.names();
                JSONArray asArray = thisobject.toJSONArray(names);
                JSONArray resultsArray = thisobject.getJSONArray("results");
                                
           JSONObject jsonObject = resultsArray.getJSONObject(0);
                    
            String twitterText = jsonObject.getString("text");
                    
            //System.out.println(twitterText);
            System.out.println("Calling rssbean from Driver");
            rssbean.updateDatabase("twitterText");
                    
}
}  

I have edited the Java classpath and added a user library for each of these as well as editing the Build Path of the project. The libraries are displayed in the list and I don't get compiler errors so at least Eclipse has recognized them. The problem comes during execution so I think something is wrong there.

Should I edit the classpath in Windows>Preferences>Java>Classpath> and add the Jars there? I have not had to do this for any other libraries before.


Solution

  • I found out you have to add any library files in the Glassfish/domain/lib directory before they're recognized by glassfish on the server.