Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Multiple Classes (Console) in Java

Tags:

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.

like image 548
ElvenX Avatar asked Mar 01 '12 05:03

ElvenX


People also ask

Can you make multiple classes in one Java file?

A single Java program can contain more than one class and there are no restrictions on the number of classes that can be present in one Java program. But each Java program should have one class declared as public to make it accessible for classes in a different package.

How do you compile Java files together?

Open a command prompt window and go to the directory where you saved the java program. Assume it's C:\. Type 'javac MyFirstJavaProgram. java' and press enter to compile your code.


3 Answers

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
like image 83
Daniel Lubarov Avatar answered Oct 19 '22 00:10

Daniel Lubarov


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.

like image 36
Stephen C Avatar answered Oct 18 '22 23:10

Stephen C


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

like image 1
Juvanis Avatar answered Oct 18 '22 23:10

Juvanis