0

I have created classes in eclipse under package "first" inside src. I will execute fine in eclipse. But when I tried to run from the command prompt it will say

"Error: Could not find or load main class CreatingThread".

But when I copy out same classes into some other folder and remove package first it will work fine.
I have set environment variables as below:

path %JAVA_HOME%\lib;C:\Program Files\Java\jre1.8.0_144\bin

classpath %JAVA_HOME%\lib\tools.jar;.;

And these are the classes

package first;

public class CreatingThread {

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().getPriority());
        System.out.println(Thread.currentThread().getThreadGroup());
        for(int i=0;i<=25;i++) {
            System.out.println(Thread.currentThread().getName()+" "+i);
        }

        MyThread myThread= new MyThread();
        myThread.setName("Child Thread");
        myThread.getThreadGroup();
        myThread.start();
        System.out.println("Done");
    }
}

Second class

package first;

public class MyThread extends Thread {
    @Override
    public void run() {
        for(int i=0;i<=25;i++){
            System.out.println("child thread "+i);
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
spa
  • 329
  • 1
  • 3
  • 15

1 Answers1

0

If your class Test.java e.g. is in src/test package than the first line in the class will be something like that package test;. You will be able to compile it directly from the test folder using javac Test.java. But executing should be done with the whole package path from outside the folder - you should execure java Test from src folder instead of test.

Hope this will help.

  • after reaching folder where java files exist i executed javac *.java then i hit java CreatingThread --it failed then i tried java first.CreatingThread -- it also failed then java src.first.CreatingThread --it also failed .But whole show will runs fine when i removed package keyword from the classes then put those classes in any folder and compile ,it will runs fine .. – spa Aug 09 '17 at 10:46
  • Run "java first.CreateThread" from src folder instead "java src.first.CreatingThread" – Mikita Berazouski Aug 09 '17 at 10:49
  • Tnq @Mikita Berazouski – spa Aug 09 '17 at 10:52