40

I'm writing a makefile that compiles a .java file in a different directory, and then I want to run it, without changing directories. I want to do something along the lines of:

$(SQM_JAVA_TOOL_DONE) : $(SQM_JAVA_TOOL)
        $(shell cd /home_dir)
        javac myjavafile.java
        java myjavafile

where the Java file is /home/myjavafile.java, and the makefile isn't running from /home.

How can I do this?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
vette982
  • 4,782
  • 8
  • 34
  • 41

4 Answers4

57

I might be misunderstanding the question, but you can compile with

javac /home/MyJavaFile.java

This will create MyJavaFile.class in /home

You can then run it by including /home on the classpath. e.g.

java -cp /home MyJavaFile

If you want to generate the class file in a different directory then you can use the -d option to javac.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • 1
    I tried `java -cp ".:lib/dropbox-core-sdk-1.7.5.jar:lib/jackson-core-2.2.3.jar:lib/javax.mail.jar" /bin Client1`, but the linker spat out: `Error: Could not find or load main class .bin`. – Andrei Nov 21 '13 at 07:10
  • @Andrei, just drop the "/bin" before Client, put all jars or folders with .classes you need between the double quotes of "-cp" after it just the main-classes name. – Alim Özdemir Mar 22 '21 at 14:06
41

Use the -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. Then, to run the program, simply include this directory in the classpath:

javac -d some/directory myjavafile.java
java -cp some/directory myjavafile
Michael
  • 34,873
  • 17
  • 75
  • 109
2

Just to add to the existing answers, you may want the --source-path flag:

--source-path <path>, -sourcepath <path>
      Specify where to find input source files

I believe this effectively sets the package root javac will compile from (i.e. <path> will be stripped from the expected package name of the files). It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, not the path passed to --source-path.

For example, to compile and run from a project's root where source is stored in src/ and you want it build in bin/:

$ javac --source-path src -d bin src/mypackage/*.java
$ java -cp bin mypackage.Main

This works even from directories elsewhere in the filesystem, e.g.:

$ javac --source-path /some/absolute/path/src  -d /some/absolute/path/bin /some/absolute/path/
$ java -cp /some/absolute/path/bin mypackage.Main
dimo414
  • 47,227
  • 18
  • 148
  • 244
0

I am using VS Code and installed java and code runner extensions. When I created new java project using the extension, it was creating the .class file in src instead of bin. To solve the issue I opened settings.json file from File > Preferences > Settings and searched for "settings" (or "code-runner"). Then I added following lines in that file.

"code-runner.executorMap": {
    "java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && java -cp bin $fileNameWithoutExt",
}

If you don`t want to see the command that runs before code file then add these lines instead:

"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.executorMap": {
    "java": "there is && clear added in the execution paramater"
    "java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && clear && java -cp bin $fileNameWithoutExt",
}

I hope this finds someone with similar issue.