2

I am writing a Java program on a Windows PC, that needs to communicate with several applications on our company's Unix machines.

The program I developed contains a Swing interface with a JButton. For the moment, when I click on the button, I can select a directory like "C:\Users\MyUserName\Documents" on my machine. Here is the code sample :

selectButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            // Choose a directory 
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File("."));
            chooser.setDialogTitle("choosertitle");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);

            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
              System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
              System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
            } else {
              System.out.println("No Selection ");
            }
        }
    });

Now I would like to select a directory in another machine working with Unix (let's say the machine name is "unix-service") instead of a directory on my local machine. So when I click on the button :

  • I need to know the group of the user in our company to continue. In a Unix terminal, we can do it with the command "echo $WORK_GROUP". I want to save this user's group as a variable "user_group"

  • I want to select a directory "workgroug/user_group/username/" on that Unix machine.

How can I do it using ssh commands and adapting my code ?

I hope everything is clear and explained, do not hesitate to ask me if you need more information. Thank you for your time.

3
  • Voting to close as too broad. You know of a possible solution (use SSH in Java) but have made no attempt to implement it. Commented Aug 17, 2017 at 14:18
  • Actually I found some examples with ssh but no one of them seems to do what I want or help me to do what I want. That's why I am asking the exact problem to have some hints. Commented Aug 17, 2017 at 14:27
  • I get what your problem is but the answer to your problem is to use a library for Java that does ssh or maybe a library that does scp or ftp. And the answers are just recommending those libraries, which can be found by doing a google search. Commented Aug 17, 2017 at 14:32

2 Answers 2

2

You can use java ssh API from following library to connect to remote box.

http://www.jcraft.com/jsch/

A good example is provided here to execute a command on remote Linux/UNIX system using SSH

http://www.jcraft.com/jsch/examples/Exec.java.html

Sign up to request clarification or add additional context in comments.

Comments

0

So, you just need to get access a unix box through JAVA. There are many libraries doing that job. Have a look here: http://www.jcraft.com/jsch/

"JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs."

2 Comments

Not reason to downvote,I was typing the question while the other one came up first. Don't lose your temper and your manners people!
I just up-voted you ;). We both have same thoughts about solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.