Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-30-2008, 06:07 PM   PM User | #1
damo.gets
New Coder

 
Join Date: Aug 2008
Location: Bismarck, ND, USA
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
damo.gets is an unknown quantity at this point
Cool simple java issue: java.lang.NoSuchMethodError: main (it's a LIE!)

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:

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

Last edited by Aradon; 12-31-2008 at 11:35 AM.. Reason: issue resolved
damo.gets is offline   Reply With Quote
Old 12-30-2008, 06:55 PM   PM User | #2
damo.gets
New Coder

 
Join Date: Aug 2008
Location: Bismarck, ND, USA
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
damo.gets is an unknown quantity at this point
Cool main(String[] args) must be static

The issue's resolution was fairly simple. I've been programming in Beans, as I've mentioned, and that was my learning environment, so I pretty much completely skipped stand-alone Java apps. When main(STRING[] args) is your entry point it must be declared static.

That means that it can only play with static variables and methods; that held me back for a few minutes, too, because I got the purpose of static confused with the purpose of final. Everything is working now, though.

There were way too many different causes for this error on the google hits that I found. Hope some people can find this if they have the same newbie error I had here.
damo.gets is offline   Reply With Quote
Reply

Bookmarks

Tags
java, main, nosuchmethoderror

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:57 PM.


Advertisement
Log in to turn off these ads.