2

Hi I am trying to learn to compile a java class with make file. My make file looks like this:

build:
    javac test_java.java
clean:
    rm -rfv *~ test_java.class
run:
    java test_java

Now I have moved the test_java.java into a folder, called classes I am trying to compile the file using a relative path, is it possible?

I have tried:

javac -d classes test_java.java

but I am getting errors:

javac -d classes test_java.java
javac: file not found: test_java.java
Usage: javac <options> <source files>
use -help for a list of possible options

I have also tried: -d ./classes and also -d /home/the/whole/path (but I would like to have the relative path), and the errors are the same. It seems to work only the

javac classes/test_java.java

What am I doing wrong?

sop
  • 3,445
  • 8
  • 41
  • 84

2 Answers2

5

Update: The original answer here was completely wrong. You must specify the path to your source files. -sourcepath is for another purpose:

Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.


Note that there are many other build tools for Java applications that are very mature and well regarded. Maven, Gradle, and Ant are the ones that spring to mind immediately. If you don't have to use make I would take a look at one of those.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • :( It seems that it is not working... I was trying to use the same IDE and not install 2 or 3 IDEs, I code in more languages. It seems I am going to use more... Thanks anyway. – sop Apr 17 '15 at 07:13
  • 1
    Using another build tool does not force you to switch to another IDE (although an IDE that supports your build tool is always better). The mentioned tools are the most common for java. People almost never use Make (anymore), see http://stackoverflow.com/questions/2209827/why-is-no-one-using-make-for-java – Didier L Apr 17 '15 at 07:37
  • 1
    It seems that it works if I add the folder too: `javac -sourcepath classes classes/test_java.java` – sop Apr 17 '15 at 08:16
0

I have similar problem in Python and again both works in same directory, but only "javac" works with relative path. So if you have HelloWorld.java in folder HelloWorld, you need to do something like this:

import subprocess
import os


path_to_java = "HelloWorld/"

compile_process = subprocess.Popen(["javac", path_to_java + "HelloWorld.java"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
compile_output, compile_errors = compile_process.communicate()

print("compile_output:")
print(compile_output)

if compile_errors:
    print("compile_errors:")
    print(compile_errors)

# get full path
script_directory = os.path.dirname(os.path.abspath(__file__))

run_process = subprocess.Popen(["java", "-cp", script_directory + "\\HelloWorld", "HelloWorld"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
run_output, run_errors = run_process.communicate()

print("\nrun_output:")
print(run_output)

if run_errors:
    print("run_errors:")
    print(run_errors)

Output: compile_output:

run_output: Hello, World!

Process finished with exit code 0

Java code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}