Forum Stats

  • 3,838,876 Users
  • 2,262,407 Discussions


java rookie needs help compiling source code

3674320 Member Posts: 3
edited May 4, 2019 7:31PM in New To Java

I am relatively new to Java programming and I have hit a snag I can't seem to fix.

my system is 64 bit windows 10.

I have this rather large bit of java code running under Tomcat 9 which does a lot of mysql database work and now I would like to get a very small part of that code running from the windows 10 command line.

I have a number of small java test programs that compile and run from the command line with no issues.

I first created a small test program which wou7ld just load the database driver and that was successfull.

I used the following command to compile my java code

java -cp C:\Tomcat_9_0_14\lib\mysql-connector-java-5.1.47-bin.jar;.

However when I added code to read a database table I now can't even compile the4 java source code. I receive the following error message

Error: Could not find or load main class

Here is the file

import java.util.*;


import java.sql.*;

public class sql2 {

    private static String mysql_username = "my_user";

    private static String mysql_user_password = "my_pass";

    private static String url = "jdbc:mysql://localhost/my_database";

    private static String db_error_message = new String();

    private class division_class {

        public int        division_id;

        public String    division_name;

        public int        conference_id;

    } // end of division_class

    private static Vector division_entries = new Vector();

    private static division_class division_node;

    public static void main (String args[])  {

        Connection conn;

        Statement stmt;

        ResultSet results;

        int    num_div;

        String query = new String();

        try {


        } catch (Exception e) {

            db_error_message = "Can't get MySQL Instance : " + e;

            System.out.println(db_error_message + "\n");


        } // CATCH

        System.out.println("Successfully loaded database driver\n");

        try {

            conn = DriverManager.getConnection(url,mysql_username,mysql_user_password);

            stmt = conn.createStatement();

            results = stmt.executeQuery("SELECT * FROM hockey_divisions");

            num_div = 0;

            while ( {

                num_div += 1;

                division_node = new division_class();

                division_node.division_id = results.getInt(1);

                division_node.division_name = results.getString(2);

                division_node.conference_id = results.getInt(3);


            } // WHILE displaying SELECT results

        } catch (Exception e) {

            db_error_message = "Database error : " + e;

            System.out.println(db_error_message + "\n");


        } // CATCH


    } // end of main

} // end of sql2

As you can see, there is a "main" class. What have I done that is so horribly wrong ?


  • 3674320
    3674320 Member Posts: 3
    edited May 4, 2019 4:43PM

    Please forgive a horrible brain cramp. I finally noticed that I was running "java" not "javac" to compile the source code.due to a mixup in the batch files I am using to run and compile the java code.

    Now when I compile my java file with the command


    I see the following error error: non-static variable this cannot be referenced from a static context

                                    division_node = new division_class();

    I do not know how to fix this error. Any suggestions ?

  • 3674320
    3674320 Member Posts: 3
    edited May 4, 2019 7:31PM

    After much google searching and experiments, I finally managed to get the code to work.