File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Running a simple java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a simple java program" Watch "Running a simple java program" New topic

Running a simple java program

Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
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 (/usr/lib/
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 (/usr/lib/
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/
at java.lang.ClassLoader.loadClass(java.lang.String) (/usr/lib/
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/
at (/usr/lib/

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??
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
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

Joined: Jun 23, 2004
Posts: 91
Yeah just:
java HelloWorldApp.class
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
When you run a program, you must not add the .class extension. e.g. java HelloWorldApp
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91

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

And the file is :
// File:

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

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
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();


// 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 (
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>" +

catch(Exception ex)
out.println("<HTML>" +
"<Head><Title>" +
"Bedrock: Error" +
"</Title></Head>\n<Body>" +
"<P>SQL error in doGet: " +
ex.getMessage() + "</P></Body></HTML>");

Is this another stupid mistake???
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
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.


Mike Noel
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
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
I agree. Here's the link:
subject: Running a simple java program
It's not a secret anymore!