I am new to Java and I want to run a simple java file on my Linux host.
I started with a simple shell command:
mkdir -p ~/py4j/examples
// ~/py4j/examples/AdditionApplication.java
package py4j.examples;
import py4j.GatewayServer;
public class AdditionApplication {
public int addition(int first, int second) {
return first + second;
}
public static void main(String[] args) {
AdditionApplication app = new AdditionApplication();
// app is now the gateway.entry_point
GatewayServer server = new GatewayServer(app);
server.start();
}
}
import py4j.GatewayServer;
~/py4j0.10.6.jar
export JAVA_HOME=${HOME}/jdk
export PATH="${JAVA_HOME}/bin:${PATH}"
${JAVA_HOME}/bin/java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
cd ~
javac -cp py4j0.10.6.jar py4j/examples/AdditionApplication.java
dan@h79:~ $ ll py4j/examples/AdditionApplication.*
-rw-rw-r-- 1 dan dan 472 Dec 22 20:59 py4j/examples/AdditionApplication.class
-rw-rw-r-- 1 dan dan 431 Dec 22 20:58 py4j/examples/AdditionApplication.java
dan@h79:~ $
dan@h79:~ $ java -cp py4j0.10.6.jar py4j.examples.AdditionApplication
Error: Could not find or load main class py4j.examples.AdditionApplication
dan@h79:~ $
You need to add classpath for AdditionApplication
as well.
java -cp py4j0.10.6.jar:. py4j.examples.AdditionApplication
Notice the :.
. :
is path separator, .
is current directory. Of course that assumes current folder is ~
,