KaareZ
KaareZ

Reputation: 645

Java run without jar

I wonder if it's possible to run a program without packaging it into a jar.

For instance we have this:

-AppRoot
    Main.class
    -Misc
        Math.class
        OtherTools.class
    -YetAnotherFolder
        UsefulFunctions.class

Is this possible? The main method should be executed from command line or similar.

Upvotes: 2

Views: 4140

Answers (2)

Kalyan Chavali
Kalyan Chavali

Reputation: 1348

You can run this way from the AppRoot directory

javac -cp Misc/*:YetAnotherFolder/* Main.java //To compile

java -cp Misc/*:YetAnotherFolder/* Main // To run

Below is some documentation

  -classpath classpath
   -cp classpath
          Specifies a list of directories, JAR archives, and ZIP archives to search  for  class  files.   Class
          path  entries  are separated by colons (:). Specifying -classpath or -cp overrides any setting of the
          CLASSPATH environment variable.

          If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the cur-
          rent directory (.).

Upvotes: 4

barthel
barthel

Reputation: 950

Add all (sub-)directories containing class files to classpath and use the class with the main method as argument of the java executable.

The directory structure is your package structure.

java -cp ./:./AppRoot:./AppRoot/Misc:./AppRoot/YetAnotherFolder AppRoot.Main

This should work if all dependencies are resolved and on the classpath.

Upvotes: 1

Related Questions