How to compile a java project consisting of multiple files? - java

I have a java project which contains multiple classes spread out over multiple files.
How do i compile and get this to work ?
Can anyone suggest how to compile this using jCreator (without using a build tool like ant)

Without using Ant/Maven etc. (and I would strongly advocate using these - a command line is unmaintainable as your project increases in complexity, and unless you script it you will have to remember how you invoked it last time when you next build) you should be able to pass all your .java files to the compiler on the command line. e.g. in Unix:
javac `find . -name \*.java'
or similar (you will likely need additional args for the classpath etc.)

You should just be able to compile each file separately and then run the class with the "main" function ie the one that starts of your program. Its probably a good idea to compile the ones that don't depend on any others first so that when each ones compiled all the classes used in that class are already compiled but I don't know if you need to do this.

Related

javac not generating class files when there are errors

I'm a hobby coder using CodeAnywhere to allow me to, well, code anywhere without having to set up a development environment. It's admittedly not the fullest featured IDE, but it gets the job done. One limitation is that I can't use Ant or Maven, so I have to figure out how to build using plain old javac (via SSH).
So I've created a little bash script that creates a file with the relative paths to all the source files, and then pumps that into javac:
find -name *.java > classes.txt
javac #classes.txt
The first line works fine and generates the anticipated output. The second one also appears to work, and even gives me compile errors (which I was expecting). However, there are a few classes that should compile, but the corresponding class files are not generated. Is this anticipated behavior? Why does it work when I manually include multiple file names as arguments (even when I include ones that generate errors), but not when I use the #file notation?

Java: Creating an executable in bash/command line from several files and packages

I've looked over the questions I could find in the search concerning javac, Java compilation and so on, but they either didn't answer this combination of questions, or the solutions didn't work.
I have a project that works and compiles fine in my IDE. However, it has to be compiled and executed through bash commands. Using an IDE, Ant, Maven or any other build tool is not an option. At best, I can use a makefile but from what I've gathered from concerning Java and make around here, this is generally a bad idea. The fact that I have absolutely no idea about Unix doesn't help the matter either.
My project consists of a good number classes split up into three packages, but as a last resort I could still dump it into one package if that'd make it any easier, but I've pretty much exhausted my options to try and solve this myself. Including the commands for Windows would be appreciated (because it would make it easier to test), but isn't necessary.
Try compiling your classes in separate package like this
javac [path to folder1]/*.java [path to folder2]/*.java
OR
In unix,You can list down all java files in a single source file say projectsource.txt
and try following command
$ find -name "*.java" > projectsource.txt
$ javac #projectsource.txt

Have a difficulty with installing a .jar library

I have just downloaded a third party java library which i need for a program i'm about to create.
But i can't figure out how to actually install the library so that i can literally type
import path.to.library;
in my java class file without having any errors.
I have looked at many tutorials and answers on StackOverflow but each of them seems to include the use of some or the other IDEs for java.
Well, i'm a bit rustic and would like to know how to make it work with notepad and the command line, coz that's what i use to make a program.
When you are compiling, include the following in your line:
-classpath nameOfJar.jar
However, once you actually switch to use an IDE, you will see the multiple benefits this approach can bring.
You don't specify a path in the import statement, just the package name.
All usable JAR files have to be specified in the classpath on commandline when starting your Java program.
You need to understand how Java's classpath works. For a comprehensive description, read the Oracle manual page on this topic. Alternatively the PATH and CLASSPATH page of the Java Tutorial.
(FWIW - it is generally considered to be a bad idea to use the CLASSPATH environment variable to set the classpath, because this is liable to lead to "nasty surprises" if you deal with software that requires different classpaths.)
If you don't use any IDE you won't have code complete. However if you know all the packages/classes/methods names/signitures you can use pure Notepad and then compile it by adding the library to your classpath (eg. using the -cp switch in the javac command when compiling)
JARs are not required to be installed. They required to be accessible at compile- and run- time. You can add jar by command line parameter or CLASSPATH environment variable. IDEs have special means for setting JAR;s location in visual manner.
You'll need to be more specific about what you have tried so far. This generally isn't something complex though, if you want to manually invoke the compiler you would do something like
javac -cp somejar.jar myclass
Once you get used to this process, it's better to automate it using a build tool such as ant or maven. Ant is a little easier to begin with, maven has some additional capabilities that make it a little more complex.

Problems compiling file on School's Unix System

COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?
You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.
First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.
You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java
There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.

Easy command line Java compile

So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not require writing out lists of the files?
Personally I think javac should be smart enough to handle
javac *
when in the folder just under the root of the package hierarchy. Is there anything in this ballpark?
Edit: The source folder structure is complex and the is no single entry class so some of the ideas mentioned so far won't work. Thanks though! Think 9 levels deep with code on many levels.
From the folder that represents the base of your package hierarchy, assuming your entry point class is called Main, in a package called app,
javac -classpath . app/Main.java
should generate the correct class definitions. The compiler will ferret out the dependencies and compile whatever other classes are needed. The class files will appear in the same directory as their source files.
If, as you say, you have 'more than one entry' class, you will have to at least identify all those top level classes from the dependency hierarchy, which can be listed as further params to javac, specifying the packages as they occur. Like so, assuming you also need to start with other.Entry
javac -classpath . app/Main.java other/Entry.java
Note that you will still have to figure out which of your classes are tops of independent dependency hierarchies, whether you are creating an ant script or doing it this way.
javac BaseProgram.java will compile BaseProgram.java from the current directory, and all classes it references that are available in source code, in the same directory tree.
If BaseProgram references Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, then they too will get compiled. Likewise if they are in a package, and the package directory is available, they will be compiled.
Provide them with an Ant script that does the build with the correct libraries on the classpath, etc. The script can also do other tasks such as building JARs, etc.
This requires that that person downloads and installs Ant, but that is not hard. (And there is nothing to stop you from providing them with an appropriate Ant distro to install. Or even sending them a distro ZIP file that has a copy of Ant "preinstalled" in the tree.)
Providing an Ant script means that you avoid them falling into Java newbie traps such as forgetting to set the classpath, being in the wrong directory, forgetting to recompile dependent files and so on. Plus, it is more "professional".
You can build a file listing all the classes you want to compile (extracted from the javac man page) -
Example - Two Arg Files
You can create two argument files -- one for the javac options and
the other for the source file-
names: (Notice the following lists have no line-continuation
characters.)
Create a file named options containing:
-d classes
-g
-sourcepath /java/pubs/ws/1.3/src/share/classes
Create a file named classes containing:
MyClass1.java
MyClass2.java
MyClass3.java
You would then run javac with:
% javac #options #classes
Or you can use *.java on the command line e.g.
javac greetings/*.java
(Again from man javac)
Or why don't you just compile the source into a jar that your customer can run using their JRE - especially considering they are not Java savvy?
A simple way would be by using:
javac *.java.

Categories