Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Eclipse: Could not find the main class. Program will exit.

Posted on 2006-11-28
13
Medium Priority
?
387 Views
Last Modified: 2013-11-23
Hi, I am a newbie using Eclipse. I created a Java application project and then added a new class to it named Connect.  I then copied and pasted the following code below into it, that I got from Microsoft (http://support.microsoft.com/kb/313100).

It compiles ok but when I try to run it I get the following error:
Could not find the main class. Program will exit.

import java.*;
public class Connect{
     private java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "pubs";
     private final String userName = "user";
     private final String password = "password";
     // Informs the driver to use server a side-cursor,
     // which permits more than one active statement
     // on a connection.
     private final String selectMethod = "cursor";
     
     // Constructor
     public Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details
     */

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    }
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }    
     
     private void closeConnection(){
          try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
       }
}

Many thanks.
0
Comment
Question by:greddin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 18033729
can you see .project and .classpath files under the project folder? - post the contents of them.
I am guessing that this new project is either not a java project or has classpath not set.
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 500 total points
ID: 18034773
You can run this program in ECLIPSE if you have this class actively in the editor.
What you describe happens when you run a progam for the first time
with the wrong file in focus.

;JOOP!
0
 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 500 total points
ID: 18034884
It happens some times,

Right click on the class or the editor, select run -> Select the class (Connect) to be run -> click run
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:greddin
ID: 18038739
Here's the contents or the .project and .classpath files:

--- .project file contents ---

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
      <name>JavaDatabase</name>
      <comment></comment>
      <projects>
      </projects>
      <buildSpec>
            <buildCommand>
                  <name>org.eclipse.jdt.core.javabuilder</name>
                  <arguments>
                  </arguments>
            </buildCommand>
      </buildSpec>
      <natures>
            <nature>org.eclipse.jdt.core.javanature</nature>
      </natures>
</projectDescription>

--- .classpath file contents ---

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
      <classpathentry kind="src" path=""/>
      <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
      <classpathentry kind="output" path=""/>
</classpath>
0
 

Expert Comment

by:geoff2k
ID: 18048791
Your .project and .classpath files look OK, but they show that you don't appear to have the Microsoft SQL Server Driver in your classpath?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18050908
do you see any errors on the project?
0
 

Author Comment

by:greddin
ID: 18054814
In order to use SQL Server connectivity, do I need to actally download and install a JDBC driver for SQL 2000?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18054909
yes, you can download it from http://jtds.sourceforge.net/
0
 

Expert Comment

by:geoff2k
ID: 18055056
The specific driver that is being referenced by the example code is here:

http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en
0
 
LVL 12

Assisted Solution

by:keyurkarnik
keyurkarnik earned 500 total points
ID: 18084117
The main problem that you are facing is NOT related to the drivers!!
If the drivers are not in the classpath, you will definitely get an error, but that would happen at compile time, or at the time it actually tries to access the driver class in case another project is being referenced. The error you will get in this case is something like :

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
To solve this problem, you will have to include the specified driver jars into your class path or in the librarires tab in build path.

BUT THE PROBLEM YOU ARE FACING WILL NOT BE RESOLVED BY IT!!!

The problem here is with your build path configs. Eclipse cannot find your class files when it tries to run the program

There are 2 ways to go about this :

1. Compile your project separately outside of eclipse using javac. Provide these class files (with proper package structure or create a jar) to build  path -> librarires section. Now refresh your project. The program should run
(I DONT RECOMMEND DOING THIS)

2. Remove all the librarires from build path, except for the JRE lib. Go to the "Project" tab and select Build Automatically. [ You could also do a Clean followed by a Build Project ]. This will generate all the class files. After this is done, you will be able to successfully run the program

[At this point you should get the driver errors, for which you need to add the driver jar to the libraries tab in the Properties-Build Path. ]

Let me know how it works out!!
0
 
LVL 35

Expert Comment

by:girionis
ID: 18339107
Sorry, I forgot to also recommend some points for keyurkarnik. Moderator please correct and do a four way split:

Split: Ajay-Singh {http:#18033729} & sciuriware {http:#18034773} & mukundha_expert {http:#18034884} & keyurkarnik {http:#18084117}

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question