Search code examples
javagithubclasspath

How do I run this GitHub java project?


I want to run this GitHub java program: https://github.com/ss2cp/AI-TicketToRide

According to the README.md it should run with src/ttr.main/TTRMain

I am trying to start the game in linux, but I keep on getting the error:

Error: Could not find or load main class src.ttr.main.TTRMain

Caused by: java.lang.ClassNotFoundException: src.ttr.main.TTRMain

I tried

java src.ttr.main.TTRMain
java src/ttr.main/TTRMain
src/ttr.main/TTRMain
java ttr.main.TTRMain
java -jar JGame.jar
java -cp . src.ttr.main.TTRMain
java -cp . ttr.main.TTRMain

from the ~/Downloads/AI-TicketToRide-master/ folder (where the README.md and .classpath file is). I also tried it from ~/Downloads/AI-TicketToRide-master/bin/ttr/main/ and ~/Downloads/AI-TicketToRide-master/src/ttr/main/

But no luck.

I am using Ubuntu 20.04 and openjdk 11.0.20.1 2023-08-24.

How do I start the program and from which directory? And should I set a classpath?


Solution

  • The following sequence of commands works for me.

    1. Clone the repository:

      git clone https://github.com/ss2cp/AI-TicketToRide.git
      

      Note: Requires Git to be installed on your computer.

    2. Navigate to the project directory:

      cd AI-TicketToRide
      

      Note: This is necessary, as the image files in the project's resources directory are loaded as files relative to the working directory.

    3. Execute the project:

      java -cp bin:lib/JGame.jar ttr.main.TTRMain
      

      Note: Requires Java 8+ to be on your path.

      Note: On Windows, replace the : (colon) in the -cp argument with a ; (semicolon). You can also replace the / (forward slash) with a \ (backslash), but that is not strictly necessary (Java will correctly interpret the value either way).

    Class-path

    The -cp bin:lib/JGame.jar part is specifying the class-path. In this case, two locations are being put on the class-path. The bin directory contains the compiled class files of the source files in the src directory. The lib/JGame.jar file contains precompiled third-party code used by the project.

    Normally, I would say the resources directory should also be put on the class-path, but that does not seem to be necessary here. It appears those images, instead of being loaded as resources like the directory name would imply, are being loaded as files. Luckily, relative file paths seem to be used, but this means that the project directory must be the working directory.

    Main class

    The ttr.main.TTRMain part is the fully qualified binary name of the main class. Note the src directory is a "source root"; it is not part of the package structure.

    The class-path will be searched for the specified main class and, if found, execute that class's main method. Since the bin directory, which contains the ttr/main/TTRMain.class file, has been put on the class-path, the ttr.main.TTRMain class will be found.