1

I'm trying to connect my basic JavaRMI app to an oracle database. But I'm stuck with the ojdbc jar and ClassNotFoundException. I can't use some IDE (teacher don't want), so I have to do all by notepad and terminal prompt. I use Ant to compile the application. There is the structure : - Build (contains all .class and stub) - lib/oracle/ (conatains the oracle-full package found on the website, with ojdbc7.jar) - src (contains all .java) - build.xml (for ant)

I founded that I must use Class.forName("oracle.jdbc.driver.OracleDriver");. But didn't work alone. So next i founded some people who said that we have to set the classpath, so I do : export CLASSPATH=$CLASSPATH:./lib/oracle/ojdbc7.jar but nothing happens. I begin to try with Maven but I've surrender because the dependencies can't be dowload easily (Oracle license).

See behind an extract of my code :

import java.rmi.server.*;
import java.net.*;
import java.rmi.*;
import java.sql.*;
import java.util.ArrayList;

public class ObjectFactoryCompte extends UnicastRemoteObject implements IObjectFactoryCompte {
    private Connection dbConnection;
    private ArrayList<Compte> instantiatedAccounts = new ArrayList<Compte>();

    public static void main(String[] args) {
        try {
          ObjectFactoryCompte serveur = new ObjectFactoryCompte();
          String nom = "compteFactory";
          // Enregistrement
          Naming.rebind(nom, serveur);
          System.out.println("Serveur enregistré avec succès !");
        } catch (Exception e) {
          System.err.println("Erreur : " + e);
        }
    }

    public ObjectFactoryCompte() throws RemoteException {
        super();
        try {
            connectToDatabase();
        } catch (ClassNotFoundException e) {
            System.err.println("Impossible de trouver le .jar correspondant au jdbc Oracle.");
            System.err.println("Fin de l'application.");
            System.exit(2);
        } catch (SQLException e) {
            System.err.println("Une erreur s'est produite lors de la connexion à la base de données avec pour cause :");
            e.printStackTrace();
            System.err.println("Fin de l'application.");
            System.exit(1);
        }
    }

    private void connectToDatabase() throws ClassNotFoundException, SQLException {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String host = "jdbc:oracle:thin:@srv:port:thing";
        String user = "usr";
        String pwd = "pwd";
        dbConnection = DriverManager.getConnection(host, user, pwd);
    }
}

Can you help me to clear this issue please ? I'm running out of idea... :/

Thanks

Nipuna Priyamal
  • 370
  • 2
  • 14
Azelytof
  • 46
  • 8
  • add the jar containing the classes you use and can't be found to your classpath – Stultuske Jan 11 '19 at 09:03
  • run as `java -cp lib:lib/oracle/ojdbc7.jar MyMainClass` – rustyx Jan 11 '19 at 09:03
  • Hi, thanks for your help but it doesn't work. I'm in the "build" folder, and it can't found the class when I put the -cp option (java -cp "../lib/oracle/ojdbc7.jar" ObjectFactoryCompte). The error is : Impossible to find or load the main class ObjectFactoryCompte. But I'm in the build folder, so I don't understand. Is the option implies something that I don't see ? – Azelytof Jan 13 '19 at 21:10

1 Answers1

0

You could add a jar to the classpath at runtime - if you'd like to do so... (which does not sound desireable, but it is still some option)

See Adding files to java classpath at runtime for details.

  • Hi, thanks for your help but it doesn't work. I've tried the 3 last and i get errors. I can't cast classLoader to URLClassLoader. I'll try to search more about this exception. – Azelytof Jan 13 '19 at 20:55
  • Dealing with classpath loading during runtime is a pretty advanced task. That's one reason you'll normally leave it to frameworks (OSGi, Wildfly and others) and just don't implement it on your own. There is pretty much that might go wrong here. I'm crossing my fingers that youÄll be able to resolve it anyways. – Tobias K. Feb 12 '19 at 09:46
  • Thanks :) In fact, I give up for Oracle, and go for PostgreSQL. So I want to close this subject as UNRESOLVED, but don't know how... :/ – Azelytof Feb 14 '19 at 16:31