Search code examples
javac

How can I execute *.class file in any folder?


this is my project tree:

.
├── README.md
├── package.json
└── src
    └── xyz
        └── pagenote
            ├── HandlerTestA.class
            ├── HandlerTestB.class
            ├── Main.class

If I am in the folder src, I can execute Main.class by

src# java xyz.pagenote.Main

It works, but I want to execute it in the current folder, I have tried

.# java ./src/xyz/pagenote/Main

It doesn't work, what can I do?


Solution

  • For the command java, the default classpath is the current working directory (.).

    So when executing xyz.pagenote.Main from the src directory, and the classpath flag is not specified, java will look for the class file in src/xyz/pagenote.class

    So to execute from a directory other than src, you have to specify the classpath like:

    $ java -classpath /path/to/src xyz.pagenote.Main