简体   繁体   中英

How to set classpath for JAR file paermanenlty

There are thousands of materials on the internet. I followed some of the trusted sources but still I'm getting this error:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

I'll quickly show you what I've tried:

Added classpath in environment variables

在此处输入图片说明

在此处输入图片说明

Then I tried adding with command prompt also with this command:

set classpath=%classpath%;C:Users\\320050772\\Documents\\task-tracker\\*.jar

在此处输入图片说明

But still I'm getting this error:

在此处输入图片说明

Either I'm not doing this correctly or I'm missing an important step here. Please correct me.

Here's is my DataService.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DataService {
    public static void main(String args[]){
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:xe","system","scott");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from emp");
            while(rs.next())
            System.out.println(rs.getInt(1)+"  "+rs.getString(2));
            con.close();
        }
        catch(Exception e){ 
            System.out.println(e);
        }
    }
}

From the java docs:

Class path entries that are neither directories nor archives (.zip or JAR files) nor the asterisk (*) wildcard character are ignored.

My advise is to replace "*.jar" with concrete .jar - File references. Please double check the output of echo %CLASSPATH% (Windows) or echo $CLASSPATH (Linux and related OS)

Java Documentation 1.8: Setting the Class Path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM