0

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.

Matthew G
  • 1,090
  • 2
  • 14
  • 23
  • just `import` maybe this helps: http://stackoverflow.com/questions/10834589/netbeans-how-can-i-include-external-jar-fileslibraries-in-the-jar-file-of-my – YAMM Oct 01 '13 at 04:37

1 Answers1

2

In Java, you would use the import statement:

package com.awesome.mycode.file1;

import com.awesome.mycode.MyCodeFunctions;

public class File1
{
    public static void main(String[] args)
    {
        MyCodeFunctions.sayYes();
    }
}

The import is unnecessary for classes in the same package.

Since sayYes() is a static function, there's a variation that would allow you to use the function without the class qualifier:

package com.awesome.mycode.file1;

import static com.awesome.mycode.MyCodeFunctions.sayYes;

public class File1
{
    public static void main(String[] args)
    {
        sayYes();
    }
}

You can use a * as a wildcard in the last position to import all classes (for the import statement) or all static members—both fields and methods&mdash (for the import static statement).

EDIT

To compile both files, your source tree should look like this:

/com
    /awesome
        /mycode
            /file1
                File1.java
            MyCodeFunctions.java

To compile, cd to the folder containing /com and use the command line:

javac com/awesome/mycode/MyCodeFunctions.java com/awesome/mycode/file1/File1.java

along with whatever other options you want (to control output, etc.) Consult the javac docs for more info.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Putting `import com.awesome.mycode.MyCodeFunctions;` at the top of the file is what I'm already doing, and it's not working. If that is the appropriate import statement, then I need to know how to tell the compiler that MyCodeFunctions is in a folder different to the one File1 is in. – Matthew G Oct 01 '13 at 04:37
  • @MatthewG - Take a look at the [Creating and Using Packages tutorial](http://docs.oracle.com/javase/tutorial/java/package/packages.html) for how to get the compiler to do what you want (particularly the subsection on [Managing Source and Class Files](http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html)). – Ted Hopp Oct 01 '13 at 04:38
  • I have attempted to run `javac File1.java ../MyCodeFunctions.java`, `javac -cp .. File1.java`, `javac -cp ../MyCodeFunctions.java File1.java`, `javac -cp ../MyCodeFunctions.jar File1.java` (with MyCodeFunctions.java compiled into a jar file), and many other variations in an attempt to get the code to compile, all without success. There aren't actually any examples of compilation commands at the links you've directed me to. – Matthew G Oct 01 '13 at 04:47
  • @MatthewG - From your example commands, it looks like you are not using the `javac` command properly (you're in the wrong directory, for one thing). I've edited my answer to show one way of doing things. – Ted Hopp Oct 01 '13 at 05:06