0

I went through all of this, but still run into the

java -classpath . HelloWorld
> Error: Could not find or load main class HelloWorld
> Caused by: java.lang.ClassNotFoundException: HelloWorld
public class HelloWorld {

    public static void main(String[] args) {
    
        System.out.println("Hello World!");
    
    }
    
}

javac HelloWorld.java runs without problems (before calling java HelloWorld)

Java version:

java --version
> java 11.0.1 2018-10-16 LTS
> Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
> Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

all commands executed in terminal from the directory, where the code is.

Operating System: MacOS Monterey 12.3.1 (21E258)

What can I do to get java up and running from command line?

aknott
  • 195
  • 3
  • 11
  • 1
    Are you running this in a terminal, from the directory that contains your source file? If not, then you have to [`cd`](https://en.wikipedia.org/wiki/Cd_(command)) to that directory first. – Jesper Apr 11 '22 at 14:19
  • 1
    Please post *real* commands and error messages along with a snippet of code – Gyro Gearless Apr 11 '22 at 14:20
  • Yes, I do @Jesper – aknott Apr 11 '22 at 14:21
  • Done @Gyro Gearless! – aknott Apr 11 '22 at 14:23
  • 2
    Did you compile your code first? `javac HelloWorld.java` and then run it with `java HelloWorld` or `java -cp . HelloWorld` or `java -classpath . HelloWorld` – Jesper Apr 11 '22 at 14:24
  • Permissions should be good as well, as I get the same result running with `sudo` in front of `java` – aknott Apr 11 '22 at 14:25
  • 3
    There are two ways of running a java class: A) you do javac + java B) you go `java SomeClass.java` directly. You are mixing those two ways ... in a way that doesn't work. – GhostCat Apr 11 '22 at 14:25
  • 1
    Sudo could have created a class file with permissions that don't allow the un-sudo user to access it. – Jeff Holt Apr 11 '22 at 14:25
  • 1
    Meaning: when you have a SINGLE class that is self contained, and can be compiled on its own, then `java ThatClass.java` compiles and runs from JAVA source. Otherwise, as said, you first need to run javac, then you invoke `java` with the CLASS name. – GhostCat Apr 11 '22 at 14:26
  • Didn't work before and after the sudo command was added. Permissions are `-rw-r--r--`owner: my local username – aknott Apr 11 '22 at 14:27
  • @Ghostcat: I did compile with javac HelloWorld.java before running java HelloWorld – aknott Apr 11 '22 at 14:28
  • 1
    Then you want to ensure that you have that file `HelloWorld.class` sitting in the directory you are invoking `java -classpath . HelloWorld` – GhostCat Apr 11 '22 at 14:30
  • @GhostCat: yes, Hello world.class is in the same folder - after compiling with javac. – aknott Apr 11 '22 at 14:42
  • I don't think I am mixing A & B @GhostCat. I'm only doing A. It was the spell checker, that removed the c here from javac. I've edited the question accordingly. – aknott Apr 11 '22 at 14:45
  • 1
    Maybe you are mixing letter case? (e.g. `java helloworld` will not work because the class is called HelloWorld?) – k314159 Apr 11 '22 at 15:03
  • @k314159 I've double checked this one multiple times. Looks good to me. – aknott Apr 11 '22 at 15:20
  • 1
    `src=HelloWorld.java;ls $src && cat $src && javac $src && java -classpath . $src` Please paste that into your terminal, execute it and post the output – g00se Apr 11 '22 at 15:22
  • HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } error: file not found: HelloWorld.java – aknott Apr 11 '22 at 15:25
  • @g00se: I guess it should have been: src=HelloWorld.java;ls $src && cat $src && javac $src && java HelloWorld – aknott Apr 11 '22 at 15:27
  • Which results in: HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } Error: Could not find or load main class HelloWorld Caused by: java.lang.ClassNotFoundException: HelloWorld – aknott Apr 11 '22 at 15:28
  • 1
    Actually no. java executable now will execute source files. Try instead `src=HelloWorld.java;ls $src && cat $src && javac -verbose $src && java -classpath . $src` – g00se Apr 11 '22 at 15:29
  • Yes, but $src is actually referring to the source file - not the compiled HelloWorld.class file – aknott Apr 11 '22 at 15:37
  • Yes, sorry you're right. So the last command just `java -classpath . HelloWorld` – g00se Apr 11 '22 at 15:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243791/discussion-between-aknott-and-g00se). – aknott Apr 11 '22 at 15:53

1 Answers1

0

With the help of @g00se in the comments, I've figured it out:

My source code HelloWorld.java and the compiled code HelloWorld.class was located in a subdirectory of /Users/<myLocalUserName>/Documents/. Moving the code above the level of the Documents directory works.

So, I've created an appropriate directory ABOVE this level (=source diretory) and a symbolic link (=target) in my existing file structure UNDER my Documents directory with

ln -s <pathToSource> <pathToTarget>

My best guess right now is, that this relates to the combination of MacOS versions and java versions, as stated above in the question.

Hope that can be helpful for anybody else, feeling like a NOOP.

aknott
  • 195
  • 3
  • 11