0

How to Run a java compiled file from a package in the Windows Command Prompt?

D:\eclipse-workspace-ghj2\ExecJava\src\pkg>javac Manager_1.java

D:\eclipse-workspace-ghj2\ExecJava\src\pkg>java pkg.Manager_1

Error: Could not find or load main class pkg.Manager_1 Caused by: java.lang.ClassNotFoundException: pkg.Manager_1

I tried to run a java compiled class from a package.
Error: Could not find or load main class pkg.Manager_1

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • If you're going to experiment, I wouldn't mess up your project tree, so: `D:\eclipse-workspace-ghj2\ExecJava\src>javac -d "%TEMP%" pkg\Manager_1.java;java -cp "%TEMP%" pkg.Manager_1` – g00se Jun 02 '23 at 10:56
  • Nothing. You can use literally what I typed – g00se Jun 02 '23 at 11:12
  • You didn't change up to the directory showing at the prompt in my command (though that's a bit of a weird error message) – g00se Jun 02 '23 at 11:29
  • That's very strange. Unfortunately I've no Windows to test that on at the moment. You could do `D:\eclipse-workspace-ghj2\ExecJava\src>javac -d bin pkg\Manager_1.java; java -cp bin pkg.Manager_1` and remove that bin dir under src later maybe. Note the switch is `-d` and *not* `-D` – g00se Jun 02 '23 at 11:45
  • Good. Of course, it's not good to be mixing class files and source files (the reason for my first form) – g00se Jun 02 '23 at 12:16

1 Answers1

1

You are in directory pkg so java can not find the package pkg there. You need to be in the top directory of your packages (D:\eclipse-workspace-ghj2\ExecJava\src\ in your case) to execute the command so Java can find the package and then the class.

aled
  • 21,330
  • 3
  • 27
  • 34