Results 1 to 5 of 5
  1. #1
    damong's Avatar
    damong is offline Member
    Join Date
    Dec 2008
    Location
    Bismarck, ND, USA
    Posts
    4
    Rep Power
    0

    Question Simple program compiles but main class is not found-- not sure why

    I have been writing in Java for a few months now; however, I still consider myself fairly new to it because all of my experience in Java has been writing Beans. I have reached the point in developing the project that I am on where I have to write a small and simple Java program in order to try to debug a portion of the Bean that I'm working on. I can't do this from within a Bean because I need to rely on console debugging output.

    I thought that I would be able to throw this together relatively quickly, but I can't get this to run due to an error of:
    dgetsman@linuxdamon:~/src/java$ java -cp . Main
    Exception in thread "main" java.lang.NoSuchMethodError: main

    at this point.

    The code is simple and I am fairly confident that it is fine except for main() not being found. I would appreciate any assistance, comments, or pointers to relevant material about this issue. Here is the code:

    Java Code:
    import java.util.Date;
    import java.sql.*;
    
    public class Main {
            private static final String dbdriver="com.mysql.jdbc.Driver";
            private static final String dburl="jdbc:mysql://localhost/amitimesheets";
            private static final String dbuser="root";
            private static final String dbpw="nevahgonnagetit";
    
            private java.util.Date startDate, endDate;
            private int month, year, patient, location;
    
            public Main() {
            }
    
            public void main(String[] args) {
              //set everything here to avoid having to mess with user I/O at this
              //point
              month = 7; year = 2008; patient = 10047; location = 1024;
              startDate = new java.util.Date(year, month, 1, 0, 0, 0);
              endDate = new java.util.Date(year, month, 30, 23, 59, 59);
    
              System.out.println("\n\nmonth: " + month + "\nyear: " + year + "\n");
              System.out.println("\n\nstartDate: " + startDate + "\nendDate: " +
                    endDate + "\n");
    
              try {
                    Class.forName(dbdriver);
                    Connection con = DriverManager.getConnection(dburl, dbuser, dbpw);
    
                    String sql = "SELECT id,starttime,endtime FROM punches WHERE" +
                      " pateitn = ? AND location = ? AND deleted = 0 AND " +
                      "starttime >= ? AND starttime <= ?";
    
                    System.out.println("\n\nsql: " + sql + "\n");
    
                    PreparedStatement stmt = con.prepareStatement(sql);
                    stmt.setInt(1, patient);
                    stmt.setInt(2, location);
                    stmt.setDate(3, new java.sql.Date(startDate.getTime()));
                    stmt.setDate(4, new java.sql.Date(endDate.getTime()));
    
                    System.out.println("\n\nstmt: " + stmt + "\n");
    
                    ResultSet rs = stmt.executeQuery();
                    //let's see what we've got in there
    
                    System.out.println("\n\n" + rs + "\n");
              } catch (Exception exception) {
                    System.out.println("\n\nGot an error:\n\t" + exception +
                      "\n");
              }
            }
    }
    Thanks in advance!

    -Damon Getsman
    Programmer/IT Customer Relations/Linux & Solaris Sysadmin * ITrx - Information Technology Consultation - Bismarck ND

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Java Code:
    public void main(String[] args) {
    is your problem. Must be static if it is to be an entry point to your application.

  3. #3
    damong's Avatar
    damong is offline Member
    Join Date
    Dec 2008
    Location
    Bismarck, ND, USA
    Posts
    4
    Rep Power
    0

    Wink main(String[] args) must be static

    I was starting to wonder about that after seeing a bunch of different examples that all had main() declared as a static. Many thanks.

    I tried just adding that to the definition, too; from the error messages that I received I'm assuming that I can't set any (is it inhereited or instance scope) variables that are set within the TestSQLNDate class and that I'll have to add another method to handle that?

    FWIW, I know that I don't need to be messing with variables in that scope in this small example; I'm just doing it this way to keep it organizationally as close to my primary Bean as possible so that the debugging output is more applicable.

    Again, thank you much, I appreciate the feedback.

  4. #4
    damong's Avatar
    damong is offline Member
    Join Date
    Dec 2008
    Location
    Bismarck, ND, USA
    Posts
    4
    Rep Power
    0

    Default

    Whoops I was mixing up static with final in my mind, too. I changed the variable types to static and it's compiling and executing just fine, now.

    Except, of course, for my logic error that I missed previously. :)

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 09:48 PM
  2. Class not found Exception
    By surendra in forum Java Servlet
    Replies: 8
    Last Post: 06-09-2011, 12:52 PM
  3. Could not find the main class, program will exit.
    By aryubi in forum New To Java
    Replies: 39
    Last Post: 02-19-2010, 10:02 AM
  4. Replies: 12
    Last Post: 12-05-2008, 05:37 AM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •