Cannot Run Java Class with Java [duplicate]

70
January 24, 2022, at 6:10 PM
This question already has answers here:
What does "Could not find or load main class" mean? (58 answers)
Closed 14 hours ago.

I want to nip in the bud that I have already been through all of the points in this excellent answer here: and will explain why they didn't work and what my problems are. I am working through a book by Robert Nystrom called Crafting Interpreters. It works through the creation of an interpreter for a simple language. This is done first in Java and then reimplemented in the second half of the book in C (ostensibly to make things "easier", but C is actually straightforward, and my experience proves otherwise).

I am literally just trying to run a simple, one-file (class?) program in Java. I have been through intelliJ, Eclipse, and finally writing in VS Code with extensions and running from the command-line and haven't been able to get any working. I am using a fresh install of JDK 17.0.2 (here) Package name and main function signature look like this:

package com.craftingjlox.jlox;
...       // Imports
public class jlox {
    public static void main(String[] args) throws IOException {
        if (args.length > 1)
        {
....
}

This is saved in a file called jlox.java. C:/.../jdk-17.0.2/bin is set as the CLASSPATH variable, as suggested. Running javac jlox.java creates a jlox.class - trying to run this with java jlox nets the error:

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

The above thread/answer suggests using the absolute path to the class in your file, so I tried again with com.craftingjlox.jlox and got:

Error: Could not find or load main class com.craftingjlox.jlox
Caused by: java.lang.ClassNotFoundException: com.craftingjlox.jlox

Maybe still not specific enough? com.craftingjlox.jlox.jlox

Error: Could not find or load main class com.craftingjlox.jlox.jlox
Caused by: java.lang.ClassNotFoundException: com.craftingjlox.jlox.jlox

Same when being more specific and actually lead it right to main:

Error: Could not find or load main class com.craftingjlox.jlox.jlox.main
Caused by: java.lang.ClassNotFoundException: com.craftingjlox.jlox.jlox.main

The answer in the thread above in Reason #2 (point 5) suggests that you can put a separator between variables on the CLASSPATH (separated by ; as I'm on Windows), implying I should put any path with a class on it. After the jdk... path, I added the path of the folder containing the .class file I am trying to run, restarted my terminal, and re-ran the above commands. So, my CLASSPATH variable now looks like this:

echo %CLASSPATH%
C:\Program Files\Java\jdk-17.0.2\bin;C:\Lox\JLox

After restarting terminal, still the same result:

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

etc

The other suggestions are for projects clearly more complex than a single file (which itself is not much more complex than "Hello world"), or typos/incorrect file structure. My files are only two folders deep and have very short names so no issue here. I am mystified that javac compiles fine but java cannot find the file I want to run that it emits into the same folder. This is the second and last time I will pick up Java - last time I wasted an entire morning off work trying to get it to find a .jar file to follow along with an algorithms book. I have never encountered a language so aggressively unusable as this.

I should also note that anywhere I use ..., I'm just truncating my filepaths here for brevity. I hope in the full path when setting variables/calling programs, etc.

EDIT:

It was suggested that I try intelliJ, create a new Maven project and let the IDE do the work for me. I had already tried this and ran into a similar issue, which is why I was trying to go through the command line in the first place. Since it seems to be symptomatic of the same issue, though, I'll explain it here:

After creating a new Maven project, pointing it to the java 17 jdk and copying in my code, I have to set up Configurations before I can run anything. It knows where the jdk is just fine, but trying to set a Main class as specified above has it making the same complains that it cannot find it. Using the file browser to search the project finds nothing and won't let me select anything.

Answer 1

As various commenters already mentioned, using javac and java on the command line is a very masochistic way to program in Java: not only does it require you to understand the specific syntax of those tools (which prompted your question), it will also make it very hard to debug your program, because attaching a debugger is trivial in an IDE, but quite an adventure on the command line.

That said, looking beneath the hood may be instructive. You have already found the definite treatment of how to use java and javac:

What does "Could not find or load main class" mean?

but you should be looking at it more closely. In particular, your problem seems to be: Reason #2a - the wrong directory is on the classpath in this answer by Stephen C.

READ ALSO
How to graph the data accumulation of an existing variable in the code in python

How to graph the data accumulation of an existing variable in the code in python

Good afternoon, I'm learning python and I am making some graphics with a file with data, dates and times My first goal was to graphic the data vs time, Magnitude vs Fulltime, only Saturday and Sundays (d2 and w) vs time, and only the 11pm to 12md of Business...

53
pyDatalog program fails on on Android Pydroid3 platform

pyDatalog program fails on on Android Pydroid3 platform

I can't get the Python program at https://githubcom/pcarbonn/pyDatalog/blob/master/pyDatalog/examples/graph

114
Segmented Control In Header

Segmented Control In Header

im trying to add a segmented tab to my header let me show you an example before i start

93
How to use jQuery.Lazy() Plugin with hash Image Tag Helper asp-append-version (in asp.net core)

How to use jQuery.Lazy() Plugin with hash Image Tag Helper asp-append-version (in asp.net core)

I am having a problem configuring jQueryLazy() to work with images that have been appended with a hash

96