0

This is the code of Interface.java I'm executing the code by following commands:

command: javac InterfaceDemo.java

I'm getting successful compilation and getting the class files.

command: java InterfaceDemo

but after running the above command I'm getting the error like Could not load or find main class InterfaceDemo

package pkg1;
import java.io.*;

interface MyInter
 {
   void connect();
   void disconnect();
 }

class OracleDB implements MyInter
{

    @Override
    public void connect() {
        System.out.println("Connecting To oracle Database...");     
}

@Override
public void disconnect() {
    System.out.println("Database operations on oracle DB Completed.");
    System.out.println("Disconnecting from oracle Database... ");
    }

}

class SybaseDB implements MyInter
{

    @Override
    public void connect() {
    System.out.println("Connecting To sybase Database...");     
    }

    @Override
    public void disconnect() {
    System.out.println("Database operations on sybase DB Completed.");
    System.out.println("Disconnecting from Sybase Database... ");
}


}


public class InterfaceDemo {

    public static void main(String[] args) throws Exception {

      Class c = Class.forName(args[0]);
      MyInter mi=(MyInter)c.newInstance();

      mi.connect();
      mi.disconnect();
    }

}
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48

1 Answers1

0

It's because of the package at the top. A package specifies the directory of the file. If your file is not present in the directory pkg1, then it can't be found. You can do one of two things: 1) Remove the package name, and javac/java the file as normal. 2) Compile your file, place the class file in a directory called pkg1, then from the parent folder of pkg1, run java pkg1.InterfaceDemo

For more information, reference How to run a .class file that is part of a package from cmd?

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52