无法找到或加载主类onjava

时间:2015-03-11 15:59:03

标签: java tomcat javac

我有以下代码,我正在尝试创建与数据库的连接。 当我运行命令时,我收到错误“无法找到或加载主类onjava ”: java -cp。 ONJava的

我能够运行 javac -classpath“C:\ CATALINA_HOME \ lib *”onjava.java 命令 我的.class和.java文件都在同一目录WEB-INF \ classes \ com \ onjava

onjava.java

package com.onjava;
import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

import javax.naming.Context;

public class onjava extends HttpServlet {

  private DataSource datasource;

  public void init(ServletConfig config) throws ServletException {
    try {
      // Look up the JNDI data source only once at init time
      Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
      datasource = (DataSource) ((InitialContext) envCtx).lookup("jdbc/testdb");
    }
    catch (NamingException e) {
      e.printStackTrace();
    }
  }

  private Connection getConnection() throws SQLException {
    return datasource.getConnection();
  }

  public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException {
    Connection connection=null;
    try {
      connection = getConnection();
      //..<do JDBC work>..
      if (connection != null) {

            String message = "You are connected!";
            System.out.println(message);


        } else {
            System.out.println("Failed to make connection!");
        }
    } 
    catch (SQLException sqlException) {
      sqlException.printStackTrace();
    }
    finally {
      if (connection != null) 
        try {connection.close();} catch (SQLException e) {}
      }
    }
  }

修改

我更改了代码并添加了公共类。但是当我运行它时我遇到了同样的错误!

package src;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import oracle.jdbc.pool.OracleDataSource;

public class onjava {

    public static void main(String[] argv) throws SQLException, NamingException {

        Properties prop = new Properties();
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
        Context initialContext = new InitialContext(prop);
        if ( initialContext == null){
              System.out.print("JNDI problem. Cannot get InitialContext.");
        } else {System.out.print("NO JNDI problemb ");}

        // Get DataSource
        Context envContext = (Context)initialContext.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup("jdbc/testdb");



        System.out.println("\n -------- Oracle JDBC Connection Testing ------");

        try {



            Connection jdbcConnection = ((Statement) ds).getConnection();
            OracleDataSource ods = ((Wrapper) ds).unwrap(OracleDataSource.class);
            jdbcConnection.close();

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        String message = "You are connected!";
        System.out.println(message);

    }

}

2 个答案:

答案 0 :(得分:1)

当像这样运行java时,您需要显式声明主类。您的命令应该看起来更像java -cp . com.onjava.onjava。但是,我也没有看到这个类中的主要方法。您需要指向main方法(即签名public static void main())。

顺便说一下,给你的类名称以大写字母(Onjava)开头是合适的惯例。

答案 1 :(得分:0)

好的,从你告诉我的内容来看,你需要在你的项目中附上一个主类。这意味着返回编译器并创建main方法。要添加代码,您需要在主类中执行类似的操作,我将调用onjavaMain

public class onjavaMain{

//Main method is this
public static void main(String[] args)
       {
       //create variable of your onjava class
       onjava onJavaConnectToDatabase = new onjava();
       onJavaConnectToDatabase.doGet(argument1,argument2);
       }
}

现在这不是完整的答案,但是这将让您了解在命令行中运行项目需要执行的操作。