-1

I have two files in the same directory namely Main.java and Functions.java

The content of Main.java is this:

import java.util.Scanner; 

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.next();

        Functions you = new Functions(s);

        System.out.println("Your name is " + you);
    }
}

And Functions.java:

public class Functions {
    public String name;

    public Functions(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }
}

I manually compile and run them on my terminal. So this is what I did:

javac folder1/folder2/*.java And after java folder1/folder2/Main.java

But it gives me an error of this:

error: cannot find symbol
Functions you = new Functions(s);
symbol:   class Functions

Even though they are on the same directory, it will still give an error.

Edit: I interchanged my code for compiling and running. Sorry for that.

John Cymmer
  • 195
  • 1
  • 9
  • First, run Functions.java file, then run Main.java – Omar Jul 15 '20 at 07:12
  • Your first command to compile `Functions` should be `javac` – Karthik Chennupati Jul 15 '20 at 07:13
  • Which version of java are you using? Newer java you can just run a .java file. In that case, it will not find Function.class because you have it in a subdirectory. It would need to be in a package in that case. I suspect you can just go to the folder `folder2` and type `java Main` and it should run the compiled version, and it will find Function.class because it is on the classpath. – matt Jul 15 '20 at 07:48
  • @matt I am using OpenJDK 13.0.1. I tried changing my working directory to where all the .java files are. Still, when I try to run in my terminal this `java Main.java`, the same error shows up. – John Cymmer Jul 15 '20 at 08:09
  • @JohnCymmer Leave off the .java, doing that assumes 'single-source-mode' avoid that at first. Check my answer, do the '.class' files exist after you run javac? If you change directory to folder2 then you can just run `java Main`, again *not Main.java but Main*. – matt Jul 15 '20 at 08:11
  • Does this answer your question? https://stackoverflow.com/questions/62872247/different-classes-in-java/62872469 – Stephen C Jul 15 '20 at 08:31

2 Answers2

0
  1. run javac Main.java
  2. run java Main

Note 1: You don't need to explicitly compile the files which are being used in the file you're compiling. If Java compiler sees, that you're using class B in the class A, Java will implicitly compile all the classes (including class B) when compiling the container class (class A, in this case).

Note 2: If you're using JDK >=11, then you can just compile+run the program with one command. JEP 330, Launch Single-File Source-Code Programs, is one of the new features introduced in the JDK 11 release. So, you can just run java ClassName.java.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • Tried it but still won't work. It still cannot find the Class but thank you for the Notes. – John Cymmer Jul 15 '20 at 07:39
  • JEP 330 is specifically about "single-file source-code programs". Since OP has spread their code over 2 files, it won't help here! And I think that's actually the problem here. – Joachim Sauer Jul 15 '20 at 07:42
  • @JoachimSauer, I know that it's about single-file. I gave him the solution for the first problem, and then, as a bonus, suggested what he might also find useful. – Giorgi Tsiklauri Jul 15 '20 at 07:51
0

The issue is classpath.

folder1
| folder2
| | Main.java
| | Functions.java

After you run javac folder1/folder2/*.java you'll have class files.

folder1
| folder2
| | Main.class
| | Main.java
| | Functions.class
| | Functions.java

Since your classes are not in a package then folder2 needs to be on the classpath. To run main.

java -cp folder1/folder2 Main

Notice that I left off the .java on the name of the class.

If you don't want to supply the -cp argument (sets the classpath) you can cd folder1/folder2 then you'll be in the folder with the class files. You can just type.

java Main

I tried this with your java files. When it works you won't see any output until you press enter.

matt
  • 10,892
  • 3
  • 22
  • 34