1

I am stuck with this error. I want to put my interface and class in separate files. But the compiler is throwing an error. Any suggestion will be appreciated

I have create an interface and class

..test\t1.java

package test;

 interface t1
{
    public void sayHello();
}

..\test\HelloWorld.java

package test;

public class HelloWorld implements t1
{
    public void sayHello()
    {
        System.out.println("Hello World");
    }
    public static void main(String args[])
    {
        HelloWorld h=new HelloWorld();
        h.sayHello();
    }
}

I tried to compile it but the compiler is throwing an error

test>javac HelloWorld.java
HelloWorld.java:3: error: cannot find symbol
public class HelloWorld implements t1
                                   ^
  symbol: class t1
1 error
Aman
  • 586
  • 8
  • 20
  • 5
    `javac HelloWorld.java t1.java` – Slaw Oct 20 '20 at 09:31
  • 1
    It compiled without any error. But How to run it? @Slaw When I tried java HelloWorld interpreter says `E:\user\java\test>java HelloWorld Error: Could not find or load main class HelloWorld` – Aman Oct 20 '20 at 09:33
  • 1
    @Slaw if I try `E:\user\java\test>java -cp . HelloWorld Error: Could not find or load main class HelloWorld ` – Aman Oct 20 '20 at 09:37
  • 5
    You need to give the fully qualified class name; i.e `cd E:\user\java` then `java -cp . test.HelloWorld`. – Stephen C Oct 20 '20 at 09:43
  • 1
    @StephenC it worked. You answer this question. I will accept it. Although slaw suggestion helped me in compilation. – Aman Oct 20 '20 at 09:45
  • 1
    Sorry, I didn't notice the package statements. But I'm glad you managed to get it working. – Slaw Oct 20 '20 at 10:53

1 Answers1

3

For compilation

I didn't include the complete path

javac -classpath e:\user\java; HelloWorld.java

Run

I didn't include the valid package name with class name for execution.

    E:\user\java\test>java -classpath e:\user\java\; test.HelloWorld
Hello World
Aman
  • 586
  • 8
  • 20