1

Please excuse the confusion, I have searched for this topic on Stack Overflow but do not fully understand the answers. I am green. I am using Eclipse to create my first Java program, it compiles and runs successfully.

    package com.java24hours;

class Saluton {
    public static void main (String[] arguments) {
        //My First Java program goes here
        String greeting = "Saluton mondo!";
        System.out.println(greeting);
    }
}

So I am trying to run This in terminal on my Mac. The file path I have is /Users/admin/documents/java/java24/com/java24hours

I try to run in the java24hours directory "Java Saluton.java" and return with a "Error: Could not find or load main class Saluton.java"

It is my understanding that this has to do with the package. I have researched package names but do not understand how the fully qualified name works with classes and packages.

While this compiles in the IDE, I would like to make it compile in terminal and understand why it did not compile the way I wanted it to.

Thank you for your patience.

2 Answers2

1

You are missing several elements.

One answer and comment pointed out that you haven't compiled the source code yet, to produce the class file. That's absolutely correct. Eclipse does that on its own, and likely puts the class files in a "bin" directory, although I'm not exactly sure what the parent folder would be, perhaps "/Users/admin/documents/java".

To compile and execute classes from the command line, outside of Eclipse, it's best to have a "build script" that builds the class files and optionally runs your classes. This would utilize "Gradle" or "Maven", or perhaps "Ant" (in decreasing order of my preference).

In any case, when you execute a class from the command line, you have to tell the Java process what "classpath" to use, and then the name of the "fully-qualified" class name to execute.

So, if you had class files written in the "/Users/admin/documents/java/bin" directory, you might be running from the "/Users/admin/documents/java" directory, and you would execute the following:

java -cp bin com.java24hours.Saluton
David M. Karr
  • 14,317
  • 20
  • 94
  • 199
0

First compile: Open a terminal and navigate to the actual location of the .Java file. IN this case /Users/admin/documents/java/java24/com/java24hours

Now write in the terminal

javac Solution.java

This whill create a new file named "Solution.class"

Second, run it

In the same terminal where the .class file was created write

java com.java24hours.Solution

That should do the trick

chntgomez
  • 2,058
  • 3
  • 19
  • 31
  • This is literally my exact answer, which has 1 downvote. – Jud Sep 07 '16 at 15:11
  • Well my bad for answering. Good luck with this one – chntgomez Sep 07 '16 at 15:16
  • Thank you for the feedback, however when I run that command (catching the typo from the word "Solution" I get the following Administrators-MacBook-Air:java24hours admin$ java com.java24hours.Soluton Error: Could not find or load main class com.java24hours.Soluton I do have the following files in this directory Saluton.Java and Saluton.class – Cache Overflow Sep 07 '16 at 15:46
  • @CacheOverflow it sounds like you are running the commands from `/java24/com/java24hours`, inside the package hierarchy, but you need to be out in the base directory `java24`. – trooper Sep 07 '16 at 16:03
  • @trooper thank you, however: "Administrators-MacBook-Air:java24 admin$ pwd /Users/admin/documents/java/java24 Administrators-MacBook-Air:java24 admin$ java com.java24hours.Soluton Error: Could not find or load main class com.java24hours.Soluton" Is there supposed to be a package in this folder? There is nothing in the java24 folder, except for a folder called "com". Thank you for the response! – Cache Overflow Sep 07 '16 at 17:43
  • Does he have to say "java com.java24hours.Solution" or can he say "java Solution" too? He is already in the java24hours directory. – Michael Apr 04 '18 at 15:58