In PHP, I can have a file like so:
function isPrime($num)
{
return true;
}
function sayYes()
{
echo "yes\n";
}
This will be in a file named functions.php in a folder named mycode.
I have another file:
include "../functions.php";
if (isPrime()) {
sayYes();
}
This is in a file named file1.php in a folder named file1. The file1 folder is inside the mycode folder.
The point of this example is that I have a file with a whole bunch of functions that I want to be able to re-use in other files. I'm going to have many folders inside the mycode folder (file1, file2, file3 etc). All of the code inside each of these subfolders is completely separate and unrelated to all of the code in all of the other subfolders. However, there are some generic functions I want to use across code in all subfolders, and as such it is placed in the top-level folder.
In Java, I have a file like so:
package com.awesome.mycode.file1;
public class File1
{
public static void main(String[] args)
{
MyCodeFunctions.sayYes();
}
}
This is in a file named File1.java in the same file1 folder as above.
I have another file like so:
package com.awesome.mycode;
public class MyCodeFunctions
{
public static void sayYes()
{
System.out.println("yes");
}
}
This is in a file named MyCodeFunctions.java, in the same mycode folder as above.
My question is this:
How do I compile these two classes such that I can reuse MyCodeFunctions.java in classes in many different sub-folders underneath the folder where MyCodeFunctions.java is located? What sort of import statement do I need to place at the top of File1.java? Do I need to compile MyCodeFunctions.java into a JAR file? If so, how? What do I run on the command line to link this JAR file in when compiling File1.java?
I understand that this is a different paradigm to the kind of include statements you find in languages such as PHP. That is the whole point of this question. I understand there is a difference, and I want to know what the equivalent way of doing it in the Java paradigm is. I do not want to use an IDE to do this, and it does not matter to me how complicated it is doing it manually.
I'm using Linux to do this, but I can just as easily do it on a Windows machine.