1

I'm Having some trouble with executing java files in my terminal (using Macbook). My original program creates text files in specific folders and opens said text file, but I will demonstrate using a simple java program, Hello.java.

public class Hello {

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

This is located in a folder, named Projects. I can run it in my IDE, and I can compile it. So now i have Hello.java and Hello.class located inside the folder.

When I try to run it with

sebastian@Sebastians-Air Projects % java Hello

I get this error message:

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

Here's some additoinal info:

sebastian@Sebastians-Air Projects % javac -version
javac 14

sebastian@Sebastians-Air Projects % java -version
openjdk version "14" 2020-03-17
OpenJDK Runtime Environment (build 14+36-1461)
OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

The java file is not in a package and the Projects folder (which has the Hello files) is located at the Desktop. I have tried javac -cp . Hello.java and then calling java -cp . Hello

Any help is greatly appreciated!

DonnumS
  • 196
  • 1
  • 15
  • Once you compile using `javac Hello.java`, are you seeing `Hello.class` file getting created in the same directory? (Not having declared inside a package isn't an issue) – Nipun Thathsara May 17 '20 at 14:44
  • 1
    If the [linked question](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) doesn't solve your problem could you [edit] your question and provide more details about the steps you tried from those answers and what you've ruled out? There are only so many causes of this error and it's very likely covered by that question's comprehensive answers. – John Kugelman May 17 '20 at 14:45
  • Yes, the Hello.class file is created inside the same directory. I will check out the other question and check if I can make it work. Will get back with more info if I fail! – DonnumS May 17 '20 at 15:48
  • I made it work, but I can't really say that i understand the reasoning. What I did is first off make a new folder in my home directory named temp ```/Users/sebastian/temp```. Then compiled it and ran it with ```java -classpath /Users/sebastian/temp Hello```. Seems like I have a lot to read about this summer. – DonnumS May 17 '20 at 16:28

0 Answers0