ElvenX
ElvenX

Reputation: 167

Compiling Multiple Classes (Console) in Java

I have 4 Classes for my project, they are (titleScreen, credits, storyScreen, and camapaign) respectively, since they are connected to each other, I don't know how to get it compiled. One more thing is that when I used the commands (DOS / CMD) javac, it still did not work, saying the compiler can not find the other classes, but they are present as a class. How can I compile it and get it to work? By the way it is in console or without the GUI, so Clean and Build in Netbeans does not work.

Upvotes: 8

Views: 39694

Answers (4)

Daniel Lubarov
Daniel Lubarov

Reputation: 7924

Just do

javac *.java

Or if you have separate source and binary folders:

mkdir bin
javac -d bin src/*.java

Or if you have multiple source folders:

mkdir bin
shopt -s globstar # requires bash 4
javac -d bin src/**/*.java

Upvotes: 22

kandroidj
kandroidj

Reputation: 13922

This answer is based on the Latest jdk7 and jre7. Meaning I have downloaded them and added them to my 'classpath' and 'path' in my computer->properties->advanced->environmental variables The answers here do not have real specifics so here is the way I found to do it appropriately: For sake of argument we will call my project 'My_Project', this holds all the source code for the project. Inside this 'My_Project' directory(folder), I created another directory(folder) called 'classes'. Now from my command line I navigated to the directory holding my source code e.g 'C:> cd Users\My_Name\Project_Location\My_Project\src' Once in the directory I compile my program using the command javac -d ../classes My_Project.java . After this executes if you navigate to your classes folder inside your project, you will see all the compiled .class files. And your program can be run from the command line e.g java My_Project while still in the classes directory Hope this helps with deploying/organizing your java applications.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718708

As others have said, some variation on javac *.java will do the trick. However, my suggestion is that you learn how to use a Java build tool:

  • The Apache Ant tool is the "moral equivalent" of the classic Make tool. You create an "build.xml" file containing the targets that you want to build in an OS independent fashion and the sequences of operations to be performed.

  • The Apache Maven tool is based on a different philosophy. Instead of saying how to build your code, you describe the code, its dependencies and the things that you want built. Maven takes care of the "how" of building ... plus lots more. This is more complicated in the short term, but (in my experience) it has lots of benefits in the long term.

Upvotes: 4

Juvanis
Juvanis

Reputation: 25950

Put your java files into a common folder, e.g. "directory", and then call javac directory/*.java from the command line.

Upvotes: 1

Related Questions