Reputation: 746
I have been using IDEs to execute java programs and I am completely new in running java programs through command prompt. File structure of my javacodes
directory looks like this:
JdbcDriverTest3
class is defined as follows:
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;
public class JdbcDriverTest3 {
public static void main(String[] args) {
Enumeration<Driver> e = DriverManager.getDrivers();
while(e.hasMoreElements()){
Driver d = e.nextElement();
System.out.println(d.getClass());
System.out.println(d.getClass().getName());
}
}
}
I have run following commands in my command prompt :
C:\javacodes>javac JdbcDriverTest3.java
C:\javacodes>java -Djdbc.driver="oracle.jdbc.OracleDriver" JdbcDriverTest3
The code should print driver class name. But it is giving nothing in response. Please suggest me the correct way of using java related commands. Thank you in advance.
Edit: The intention of using java code mentioned above is to know how driver class colud be loaded through command prompt.
Upvotes: 0
Views: 178
Reputation: 1269
Try using semicolons like:
java -cp ;ojdbc14.jar; -Djdbc.drivers=oracle.jdbc.OracleDriver JdbcDriverTest3
The code will be:
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
public class JdbcDriverTest3 {
public static void main(String args[]) {
//Class driverClass = Class.forName("oracle.jdbc.OracleDriver");
//DriverManager.registerDriver((Driver) driverClass.newInstance());
Enumeration<Driver> e = DriverManager.getDrivers();
while(e.hasMoreElements()){
Driver d = e.nextElement();
System.out.println(d.getClass());
System.out.println(d.getClass().getName());
}
}
}
Compile and run like this:
C:\javacodes>javac JdbcDriverTest3.java
C:\javacodes>java -cp ;ojdbc14.jar; -Djdbc.drivers=oracle.jdbc.OracleDriver JdbcDriverTest3
Out put:
class oracle.jdbc.driver.OracleDriver
oracle.jdbc.driver.OracleDriver
Upvotes: 1
Reputation: 31269
The name of the system property is jdbc.drivers
with an s
at the end. Change your command line to:
java -Djdbc.drivers=oracle.jdbc.OracleDriver JdbcDriverTest3
You can find more details in the Javadoc of java.sql.DriverManager
The above assumes that you have set up your CLASSPATH environment variable correctly. Given the directory structure from your screenshot, you can also, as a quick test, try:
java -cp ojdbc14.jar:. -Djdbc.drivers=oracle.jdbc.OracleDriver JdbcDriverTest3
Upvotes: 1