Skip to content

using another .java main function #1692

Closed

Description

@fdq09eca

I have the following file structure

.
├── README.md
├── lib
└── src
    ├── App.java
    └── MainProgram.java

App.java

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

MainProgram

class MainProgram {

    public static void main(String[] argv){
        App.main(argv);
    }
}

i try this in the command line at src/directory
java MainProgram.java

it is the result

~/Documents/SW_assignment/assignment/src » java MainProgram.java
MainProgram.java:4: error: cannot find symbol
        App.main(argv);
        ^
  symbol:   variable App
  location: class MainProgram
1 error
error: compilation failed

How to print out Hello world! ? Thank you.

Activity

  1. fbricon commented on Nov 5, 2020

    @fbricon
    Collaborator

    If you're trying to run your code from a terminal, you need to compile all the classes in the terminal too, using the javac compiler:

    ❯ javac *.java

    That will generate the .class files in the same directory, you can then execute:

    ❯ java MainApp
    Hello, World!

    When in vscode, the compilation happens behind the scene, the class files are hidden from you. You need to install the Java Debugger to run your code. Then simply click on the Run or Debug link over the main method: https://youtu.be/o4BTKD3iyYE?t=45

  2. fbricon commented on Nov 5, 2020

    @fbricon
    Collaborator

    With recent JDKs, calling java MainProgram.java works fine to compile and execute single files in one command. It just doesn't also compile dependent files as well (that I know of)

  3. fdq09eca commented on Nov 5, 2020

    @fdq09eca
    Author

    With recent JDKs, calling java MainProgram.java works fine to compile and execute single files in one command. It just doesn't also compile dependent files as well (that I know of)

    true, java App.java works fine. extra question: any hotkey for the java run or debug?

  4. testforstephen commented on Nov 6, 2020

    @testforstephen
    Collaborator

    F5 -> debug
    Ctrl + F5 -> run

    It will run the Java file in the current active editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions