4

I want to compile .java file with MYsql JDBC Connector

This is where the .jar file is

D:\mysql-connector-java-5.1.31-bin.jar

This is what I used to compile...

javac -cp "D:\mysql-connector-java-5.1.31-bin.jar" LocationServer.java

Code for LocationServer.java

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

public class LocationServer  {

private static final long serialVersionUID = 1L;
private Connection conn;
private final String driver = "com.mysql.jdbc.Driver";
private boolean connection;

protected LocationServer() {
    try {
        Class.forName(driver);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Driver Found");
    location = null;
    x = null;
    y = null;
    conn = null;
    connection = false;
}

public static void main(String[]args){
    LocationServer ls = new LocationServer();
}

When I run the the code from CMD ClassNotFoundException throws error.

How can I properly connect a .jar file with LocationServer.java so that MySql Driver class is found?

Karedia Noorsil
  • 430
  • 3
  • 9
  • 21
  • http://stackoverflow.com/questions/9823729/setting-the-classpath-for-jar-files – Thusitha Thilina Dayaratne Aug 07 '14 at 15:37
  • When getting an exception at runtime rather than an error message from the compiler, you most probably *have* compiled your class. So you are asking the wrong question. You want to know how to *run* your code correctly. – Holger Aug 07 '14 at 17:21
  • You don't need the driver on the classpath at compiletime (unless you directly use interfaces or classes from the driver), you only need it at runtime. – Mark Rotteveel Aug 08 '14 at 09:34

2 Answers2

9

Well if you are using command prompt you can do like this Compiling the class

javac LocationServer.java

Executing the class

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

remember it will be ; but not :

SparkOn
  • 8,806
  • 4
  • 29
  • 34
5

in the last commande it's : not ;

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

java -cp .:completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

JFouad
  • 551
  • 6
  • 12