• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running a simple java program

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I'm having a slight problem and I cant figure it out. My program is compiling but wont run. The working directory is in the class path as is the necessary
java files.

The error I'm getting in the console is below:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.class
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./,file:/usr/java/jdk1.5.0_07/,file:/usr/java/jdk1.5.0_07/lib/tools.jar,file:/usr/java/jdk1.5.0_07/jre/lib/rt.jar,file:/jakarta-tomcat-5.5.9/common/lib/servlet-api.jar,file:/jakarta-tomcat-5.5.9/common/lib/servlet.jar,file:/jakarta-tomcat-5.5.9/common/lib/jsp-api.jar,file:./,file:/home/nguy/java/,file:/usr/local/pgsql/share/postgresql-8.1-407.jdbc3.jar,file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/libgcj.so.6.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.6.0.0)
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)


And the contents of the file is:

/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/

class HelloWorldApp {
public static void main(String[] args) {
//Display "Hello World!"
System.out.println("Hello World!");
}
}

Any help??
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works just fine for me (running from an IDE). How are you trying to run it, from a command line?
 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah just:
java HelloWorldApp.class
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you run a program, you must not add the .class extension. e.g. java HelloWorldApp
 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh!

Thats it.

I went back to this to try solve a problem I was having with creating a servlet that accesses a postgrelsql database.

Im getting the following error when accessing it using tomcat :
javax.servlet.ServletException: Class not found Error
ShowBedrock.init(ShowBedrock.java:37)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:369)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


And the file is :
// File: ShowBedrock.java

/* A servlet to display the contents of the PostgreSQL Bedrock database */

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet
{
public String getServletInfo()
{
return "Servlet connects to PostgreSQL database and displays result of a SELECT";
}

private Connection dbcon; // Connection for scope of ShowBedrock

// "init" sets up a database connection
public void init(ServletConfig config) throws ServletException
{
String loginUser = "postgres";
String loginPasswd = "supersecret";
String loginUrl = "jdbc ostgresql://localhost/bedrock";

// Load the PostgreSQL driver
try
{
Class.forName("org.postgresql.Driver");
dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
}
catch (ClassNotFoundException ex)
{
System.err.println("ClassNotFoundException: " + ex.getMessage());
throw new ServletException("Class not found Error");
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
}

// Use http GET

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html"); // Response mime type

// Output stream to STDOUT
PrintWriter out = response.getWriter();

out.println("<HTML><Head><Title>Bedrock</Title></Head>");
out.println("<Body><H1>Bedrock</H1>");

try
{
// Declare our statement
Statement statement = dbcon.createStatement();

String query = "SELECT name, dept, ";
query += " jobtitle ";
query += "FROM employee ";

// Perform the query
ResultSet rs = statement.executeQuery(query);

out.println("<table border>");

// Iterate through each row of rs
while (rs.next())
{
String m_name = rs.getString("name");
String m_dept = rs.getString("dept");
String m_jobtitle = rs.getString("jobtitle");
out.println("<tr>" +
"<td>" + m_name + "</td>" +
"<td>" + m_dept + "</td>" +
"<td>" + m_jobtitle + "</td>" +
"</tr>");
}

out.println("</table></body></html>");
statement.close();
}
catch(Exception ex)
{
out.println("<HTML>" +
"<Head><Title>" +
"Bedrock: Error" +
"</Title></Head>\n<Body>" +
"<P>SQL error in doGet: " +
ex.getMessage() + "</P></Body></HTML>");
return;
}
out.close();
}
}


Is this another stupid mistake???
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said you got this latest error when running from within tomcat. I'm assuming, then, that you have a compiled and deployed webapp. Does your webapp war file include the j2ee.jar library? That's where the ServletException class is defined.

_M_
 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have other servlets running ok its just this one.
My tomcat logs say:
ClassNotFoundException: org.postgresql.Driver
when I access the page so I think its to do with the postgresql??
Am I right?
I have the postgresql.jar file stored and referenced in the classpath but it still doesn't seem to want to work
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic