-1

I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.

Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:

package com.hello.programs;

public class ABC {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

Then I compile it in the usual way:

C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java

Later, run it by giving the package name and then my java class name:

C:\SimpleJavaProject\src > java com.hello.programs.ABC

In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.

It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?

Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.

cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java

--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs

Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    Note that a program as simple as yours can be run as single-source file using just `java ABC.java` which will compile and run the program in one go. – Holger May 31 '21 at 10:31
  • @Holger I am wondering if you just create a simple app and test your answer in your PC? – Jack Jun 02 '21 at 13:44
  • 1
    Why do you think I didn’t? – Holger Jun 02 '21 at 14:28
  • Because none of the suggestions are working and if there is not a missing poiğnts, you should not tried. – Jack Jun 02 '21 at 15:53

2 Answers2

1

java com.hello.programs.ABC

When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.

This must mean that either:

[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.

[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.

Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 1
    So go to the directory you already show in this question (The `src` dir, __not__ `src/com/hello/programs`), and once you're there, run `java com.hello.programs.ABC`). If that doesn't work, run `java -cp . com.hello.programs.ABC`. – rzwitserloot May 31 '21 at 22:15
  • Your example is close to work, but you did not give the directories where I should run the commands. So, there are some unnecessary folders created. I am wondering if you just create a simple app and test your answer in your PC? – Jack Jun 02 '21 at 13:44
  • 1
    That's because the command is to be run in the exact directory your question already posited. Just do what you, yourself, said you did. Follow your own instructions. I have, it works fine. – rzwitserloot Jun 02 '21 at 16:50
0

When you use javac ABC.java you are compiling the class, and javac places it in the current directory.

So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).

You can use the javac -d flag:

-d <directory>               Specify where to place generated class files

For instance:

> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class

Would work, because javac did place ABC in com/hello/programs.

Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.

Sebastian
  • 142
  • 2
  • 7
  • Your example is close to work, but you did not give the directories where I should run the commands. So, there are some unnecessary folders created. I am wondering if you just create a simple app and test your answer in your PC? – Jack Jun 02 '21 at 13:44