0

I created the below project structure.

[~/Projects/CountWords]@Ubuntu1804  #> find .
.
./bin
./src
./src/main
./src/main/java
./src/main/java/App.java

This is App.java content :

package src.main.java;


public class App {
    public static void main(String[] args){
        System.out.println("Hola");
    }
}

I compile my app as :

javac -d bin/ -cp src/main/java/ src/main/java/*

Which works fine. I am using an asterisk because I'll have more classes in that folder but I have only that one for now. This creates the following structure :

[~/Projects/CountWords]@Ubuntu1804  #> find .
.
./bin
./bin/src
./bin/src/main
./bin/src/main/java
./bin/src/main/java/App.class
./src
./src/main
./src/main/java
./src/main/java/App.java

But my problem is when I try to run the app. I assumed this would work :

java src.main.java.App

But this fails with :

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

What am I doing wrong?

I know that it must be a very silly question, but I want to code a small/medium project by hand just to understand what the IDE does on its own. After working with Java for a while using Eclipse, I noticed I had no idea how to do this by hand and I want to change that.

4
  • Where do you run java? At project root or at bin/ ? Commented Feb 26, 2020 at 16:51
  • At the root level. Which is /home/matias/Projects/CountWords. If I try to do this : java -cp bin/ src.main.java.App I get this error : Commented Feb 26, 2020 at 16:52
  • Error: Main method not found in class src.main.java.App, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application Commented Feb 26, 2020 at 16:52
  • @SantosshKumhar None of those commands worked. Commented Feb 26, 2020 at 18:02

1 Answer 1

1

You have to define class path for java. There are two possibilites:

run

java -cp ./bin src.main.java.App

or change to bin directory and then run without cp

cd bin 
java src.main.java.App
Sign up to request clarification or add additional context in comments.

3 Comments

Error: Main method not found in class src.main.java.App, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Do You still have the same code like posted above in your current App.java? Because I tried it out and it compiles and runs fine. I would suggest you remove everything else you have in your src/main/java and just leave App.java there and compile and execute again.
You are right! I rebooted and now it works! Thank you!

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.