0

I'm programming my first 2D game in java. I'm writing the code in Atom. I compile and run the code from the terminal on my Mac. The game works great so far! But I fear I am structuring my project incorrectly and will not be able to deploy it when it comes time. I want to know the proper way to build a directory structure for a project like a 2D java-based game, ideally, doing it from scratch.

I put the game making on hold to figure out structuring. I tried using LibGDX as a means of packaging my project, and then importing it as a Gradle project into Eclipse. I suddenly found myself with a huge and unclear directory structure surrounding my once-simple project. I felt the task was no simpler than structuring the project myself from scratch. In addition, I could not get any simple java code to run from within this auto-generated directory structure provided by LibGDX, now inside Eclipse. I would prefer to write all code in Atom, and compile and test the code from the terminal, creating appropriate JARs when necessary, again from the terminal.

I decided to simplify the task at hand and return to the Oracle docs, and I determined that what I'm trying to build is a self-contained java application. Sweet! But the Oracle docs don't seem to go too into depth on the logic behind the necessary directory structure needed for such a project. They tell you the two main components you need are the resources for your application (so the .java files, images and audio, etc.) and a copy of the JRE so that the user has everything they need to play the game when they execute the program. I just don't feel confident with how to set up this directory structure with the information provided.

I would like to overcome this "how should this all be structured?" problem so I can return to developing the game. Thanks in advance for reading and providing feedback.

1 Answers1

0

The industry standard for structuring Java projects is to use a tool like Maven or Gradle. Both of these tools automatically handle wrangling all of your source code, resource files, etc. and have systems to make it easy to export jar files (from an IDE or command line). In essence, they allow you to specify anything that you want for how your Java project is built (Gradle can also do C++ projects), and then they provide an abstracted command line interface to build the project.

It is important to note that your preference of working with Java without an IDE is not shared by many developers. While this isn't in and of itself a problem, it becomes one when you want to work with others who will be using an IDE. Gradle and Maven both make this easy as almost all IDEs will work well with Gradle and Maven projects. For this reason, most FOSS Java projects use one of these systems (usually Gradle) to allow different developers to use their preferred IDE.

You also expressed a desire to fully understand the set of files that make up your project. For this, Gradle is probably your best bet as its tutorials do a very good job of explaining this. It is more complex than a plain Java project, but it is worth it to most developers as you get very easy and expressive tools (eg. 3 lines of Gradle configuration can create a jar export) along with the ability to use many development stacks (eg. an IDE, command line, and continuous integration) at the cost of a minor learning curve. Gradle is also the current most popular non-IDE-dependent Java build tool. Maven acts a bit more like a black box, but still has many of the advantages of Gradle. If you want to learn Gradle, their getting started guides are a great place to start.

john01dav
  • 1,842
  • 1
  • 21
  • 40
  • Thanks john01dav. I was able to follow the steps provided on the Gradle getting started guide you suggested, and I was able to build the directory structure and run a simple 'Hello World Program' Java program using nothing but the Gradle commands. It's definitely pointed me in the right direction. – mark getrost May 27 '19 at 20:42
  • Would I be able to store a copy of the JRE, as required by the self-contained java application model, in a repository in the build.gradle file? I assume the JRE would be considered a dependency, and that the type of repository that I require would be a flat-directory repository, so that it would be stored somewhere in my root application folder, ready to use when the user launches my game. It seems Gradle will help in structuring my program, but the Oracle use Ant tasks to do this importing of the JRE. I assume the same can be done here with Gradle. – mark getrost May 27 '19 at 22:05
  • @markgetrost I am mainly a C++ programmer, so I don't know all of the details of Gradle, but it is probably possible to have Gradle output a zip containing the JRE and application jar(s). I would look at those tutorials or ask another question to get more details on how to do that. In general, it is preferred to have all dependencies (including the JRE) imported by the build system when the application is built, instead of including them in the application's repository or gradle project. – john01dav May 28 '19 at 02:21