V
V
Vanes Ri_Lax2015-04-02 10:39:13
Java
Vanes Ri_Lax, 2015-04-02 10:39:13

How to run a java program on centOS?

Hello, help me solve the problem,
There is computer 1 on it with os windows installed. On this computer, I wrote and assembled a simple java program in netbins. The essence of the program is to connect to a remote server via ftp and display a list of files on the screen. Here is the program code:

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
public class Main {
    public static void main(String[] args){
        try {
            FTPClient ftp = new FTPClient();
            ftp.connect("host");
            ftp.setDefaultPort(21);
            ftp.enterLocalPassiveMode();
          ftp.login("user","pass");
           String[] list = ftp.listNames();
            for(int i=0; i<list.length; i++){
                System.out.println(list[i]);
            }
           ftp.logout();
        } catch (IOException ex) {
           Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

There is a computer 2 on it costs centOS. I copied the jar file to it, connected to it via SSH and typed there java -jar file.jar
. After a while, the program displays the following message:
Apr 02, 2015 11:19:40 AM Main main
SEVERE: null
java.net.ConnectException: Connection timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:894)
        at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:759)
        at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2825)
        at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2876)
        at Main.main(Main.java:21)

Further, on computer 2, I checked whether there is access to the remote FTP server at all, I type telnet host 21
The most interesting thing is that the remote server answered. And from my program it is not connected.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Khabarov, 2015-04-02
@vanesxl

The FTP protocol uses more than one port.
21 is the "Control Connection".
To transfer data (listing also applies to this) a separate connection is used, which is called "Data Connection".
In active mode, this connection is initiated by the FTP server. Which address and port to connect to - the client informs when opening a connection. This transfer mode is currently not recommended.
In passive mode, the connection is initiated by the client, and the address and port (dynamic, changing) are reported by the FTP server.
You need to enable more detailed logging in the FTP client so that the commands sent to the FTP server and the responses received are displayed.
Well, check if the connection to ports other than the 21st is forbidden somewhere.

M
mihzas, 2015-04-02
@mihzas

telnet does not use ssh
try the same with putty

V
Vasily, 2015-04-02
@Applez

On linux, ports 0-1000 can only be opened as root. Most likely this is the problem, you are simply not allowed to open the port. Try to run the code as root, if it works, then tune in.

M
Mr Jenkins, 2015-06-23
@qassarb

You can also check the network settings for the JVM

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question