2
bash
/Desktop/Lab 3$ cd Stemmer
/Desktop/Lab 3/Stemmer$ java Stemmer
/Desktop/Lab 3/Stemmer$ cd ..
/Desktop/Lab 3$ java Stemmer/Stemmer
Error: Could not find or load main class Stemmer.Stemmer
Caused by: java.lang.NoClassDefFoundError: Stemmer (wrong name: Stemmer/Stemmer)
/Desktop/Lab 3$ 

Why doesn't java run Stemmer when I specify a relative path?

It seems to have run the program when I was in the directory, but I want java to run Stemmer as it did when I was in the directory that it was located in.

Could someone explain what is happening here please.

2
  • Do you have a file named Stemmer.java? What is in this file? Commented Jan 6, 2021 at 21:52
  • 1
    I see I made some assumptions about what you are trying to do by answering below. FYI, it helps a lot when you ask a question to include all the necessary details for us to reproduce the error you are asking about. In this case, we need to see the contents of Stemmer.java at the very least. Also, the detail about how you are writing a Linux script to run this Java program would give some context that you can edit into the original question here. Commented Jan 6, 2021 at 21:56

2 Answers 2

2

When you run a Java program, you specify the class name including the fully specified package, not the file path. This means that you separate the package "path" with dots, not slash. If you compiled a file named Stemmer.java that is in a folder named Stemmer and has package Stemmer as the first line, then you do

java Stemmer.Stemmer
Sign up to request clarification or add additional context in comments.

2 Comments

Typing java Stemmer.Stemmer just does Error: Could not find or load main class Stemmer.Stemmer Caused by: java.lang.NoClassDefFoundError: Stemmer (wrong name: Stemmer/Stemmer), as before.
@SuperS It sounds like you have a file named Stemmer.java but it doesn't have a class Stemmer declared in it. And do you have package Stemmer at the top of the file?
1

There are 2 broad options:

You are trying to write an application in java

Then you need an IDE to develop it in, and a build system to produce the distributables, in the form of a jar file. There is no point or reason to trying to run this 'on the command line' the way you are. You need packages and a project definition.

You are just toying around for now, writing some basic code in a single source file.

Then just run the source file. This is a feature introduced in, I think, java11. Before that, this model of writing (stuff some lines in a source file and run it right away) is not what java itself is good at, only IDEs do that properly.

Starting with java11:

java Stemmer/Stemmer.java

works great, and no need to (re)compile anything. java will take care of it.

Explanation

When you run a Java program, you specify the class name including the fully specified package, not the file path.

You read this answer before, and then proceeded to completely ignore it and try java Stemmer.Stemmer which obviously doesn't work.

The class you have is named Stemmer, and it is in the unnamed package. Thus, to run it, java Stemmer is how to do this. It's not a file name. Stemmer.Stemmer is not java-ese for 'run the class file Stemmer in the subdirectory Stemmer, and 'package' is not java-ese for 'directory on the filesystem'.

The classpath root for your Stemmer class is its own directory, as you are not using any packages. the default classpath is the current directory. It is not possible to run the Stemmer class file without having its root on the classpath, so if /Desktop/Lab 3/Stemmer is not on the cp, you can't do it. So let's fix this:

java -cp '/Desktop/Lab 3/Stemmer' Stemmer

and that'll work fine.

More generally, using the unnamed package is a bad idea, and trying to run raw class files is similarly a bad idea - use an IDE for development, and a build system to build projects.

These rules and caveats all make perfect sense when writing a 'real' project (one you check into source control, and eventually deploy someplace or ship as a product to other users). But it's onerous and a tad ridiculous if just messing around. That's exactly why (these days) you can just specify a path to a java source file, which seems to be what you want to do. So, do that.

2 Comments

Thank you. java Stemmer/Stemmer.java seems to work ok. Just thought I'd mention that the Java program I am trying to run wasn't written by me, and I am trying to run it as part of an executable "shell script" in Linux. It seemed like it would be inefficient to move into the directory to call files outside the directory to use them with the program, which is why I wanted to call the program from outside the directory. Sorry I didn't make that clear in my question.
@SuperS Then the solution is to set the CLASSPATH environment variable, either with export or with the -cp flag as described in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.