Home > Enterprise >  How to run java file on remote server
How to run java file on remote server

Time:09-27

I have a Java file server.java on my local machine, how could I run it on my school server, which is [email protected]? I have tried the command below but it didn't work.

ssh username@cs2.liv.edu server.java

CodePudding user response:

Take a look at jsch. JSch allows you to connect to an sshd server and execute command(s), there.

Here are sample examples:

https://github.com/is/jsch/tree/master/examples

CodePudding user response:

First of all, check that Java is installed on the remote server, and what version is installed.

 $ ssh  username@cs2.liv.edu java -version

(If the remote server tells you that the java command is unknown, then talk to the admins about getting Java installed and / or what you need to do to access the installed version.)

Next check what the version number is:

  • If the Java version is 9 or later, then you should be able to do this:

    $ ssh username@cs2.liv.edu java server.java
    
  • If the Java version is 8 or earlier, then you must compile the Java class first:

    $ ssh username@cs2.liv.edu javac server.java
    

    Then you can run it as follows:

    $ ssh username@cs2.liv.edu java server
    

Note that the above instructions assume that the class in "server.java" has no dependencies on 3rd-party libraries, etc, and that it is in the default package. If these assumptions are incorrect, then apply the solutions that you would use to compile and run the class on your local machine.

The other alternative to compiling the code on the server is to create a executable JAR file locally, copy it to the remote server and run it there. (Search for resources on creating an executable JAR file.)

  • Related