Java Remote File Options - java

I've recently started an internship and I'm having some issues with Java in the enterprise environment. I've been assigned the task of porting the functionality of an old windows shell script to java so it may be ran on one of our JVMs.
The shell script runs on one of the Windows servers and grabs the previous days log files from a couple of linux boxes then FTPs them to another Windows machine.
I'm not quite sure how to achieve this functionality using java. I only can SSH into the linux boxes. I have got the connection working for the Windows file share, just not sure of the best way to connect to the linux boxes.
I've considered using one of the SSH libraries, but would prefer to avoid using a third party library.
Any insight would be great. Thanks!

Java development is all about the appropriate use of third-party libraries. You can try and avoid it, but you won't get anywhere.
I suggest using JSch for your SSH/SCP/SFTP needs.

You're looking for SFTP. Look at How to retrieve a file from a server via SFTP?.

Try using SCP (an FTP-style way to transfer files, but it uses SSH) to transfer the files from Linux.

Related

Java remote interactions

I'm in internship, and my mission is to build a deployment program in Java. My approach was based on a "tasks to do for a deployment" model with dynamic instanciation (but this is not the point). Every task does something locally or on the remote server (sometimes both). For example, I have a Copy task which copies a local file to the remote server. Got some tasks like this for basic interactions (Move, Delete, ...).
The point is, each deployment module is a server which has different parameters (OS, connection params, ...) and basic programs. One of them is a Linux server, so I figured I could use SSH (with JSCh) to do the job. But the other is a Windows machine, with no SSH whatsoever. I searched everywhere for several days to find what could be used, with no results.
So here is my question:
what Java API should I use to get the job done?
Is it possible with Telnet or FTP?
Is it possible at all without ssh ?
Little detail: I don't have any control on remote servers, I can't install new protocols or programs, nor have a RMI server launched on them.
Thanks for your answers.
There are a few options to interact with a remote Windows server. If your local Java application is running on Windows (that is, both your source and target server are running Windows), you can spawn a new process and execute PowerShell commands - see this link for an example.
There are ways to copy and delete files on a remote server using PowerShell. Since you didn't fully specify your requirements, I don't know if that covers all your use cases or not.
If your application does not run on Windows, you'll have to resort to a pure Java solution like j-Interop to connect to the remote system using DCOM / WMI. See this page for a good introduction to WMI and j-Interop. Word of warning though - getting DCOM to work requires some configuration changes, specifically changing permissions on registry keys, on modern versions of Windows (Windows 7 and up).

how execute linux command using java on windows?

I need execute bash commands on a separate Linux machine using java on Windows.
I need run executable file from specified directory, like
cd /home/bin
How can I do this?
You will need to use some kind of SSH library for Java that you can then use to create an SSH connection to the target machine and run the commands inside of the SSH session.
Here is another SO question that covers this. And here is another.
After long hours, I finally found useful information. please follow link "How do I run SSH commands on remote system using Java?"
Also use jar from "http://www.ganymed.ethz.ch/ssh2/" the link. It is the jar required for any person to execute ssh commands from java code in windows environment. There are other ways too. And most jars I didn't find them useful, especially when you want to automate user authentication to access linux environment.
Note, passing directly the password (ssh password) is not appropriate way of doing things.And many experts suggested in few other threads.But this simple solution works for someone who is not concerned about security.

Best way to copy a file from Windows machine to Linux machine using java

My requirement is to copy a file that is on windows machine to Linux machine using the IP, User name and password of the windows machine using java code. I have the java code running on linux machine. I want to know the best way to achieve this. What is the procedure to achieve this?
You can use JCIFS to access a Windows share from Java on Linux or Windows.
Your code is running on the Windows machine, right? The most secure way would be to use SSH. http://www.jcraft.com/jsch/ looks like a pretty good implementation. It's used by ant, maven. etc, to do the same thing.
On the other hand, if you're not creating a new build system, could I suggest that perhaps your build system (ant/maven/gradle) should be copying your files around for you?

Create an installer in Java

I want to create an installer using Java that install at first MySQL. The user tape at first the password of root user. Then the installer copy jar file into program files and create shortcut on desktop.
So my question how to install MySQL automatic via Java. Is there any way??
Thanks in advance.
best regards,
Ali
Depends on the platforms you want to install onto. Basically if you know how to do it via the command line, then you can write a shell script that is executed from Java, or a series of command line statements that are executed from Java to do it.
Since you mention root user, I'm guessing some flavor of linux? Doesn't MySQL already have ways of doing this that come with the installers and/or binary distributions?
This write-up might help you in creating a java installer: Convert Java to EXE (also has information about other platforms)
But, before going for that, I would like to ask you, why do you want o bundle MySQL with your java app? The recommended way, if you want a DBMS bundled in your app can be:
Ask the user the install MySQL him self. You app will use it.
Use SqLite (embedded RDBMS). Or even simpler, Berkley DB for a Key-value store. This approach will be super light and no installation needed.
You can try to perform a 'private' ad-hoc MySQL installation that is only used by your application: that means you will have to copy the binaries (please note they are different for each platform) plus some custom configuration files to a 'mysql' subdirectory of your programs' main directory.
I can assure you it won't be easy and fast to do. You have to struggle a bit making it work under each platform. This kind of stuff is always a bit tricky.
If you prefer to install MySQL in a system-wide manner (as a service, using the provided install package) you'll have to embed the package into your setup program and then use the proper operating system commands to install it. That would be different on each platform, and under Linux you'll have to install the proper package for each distro. Messy.
You can look at some commercial solutions for making Java install programs. See install4j for example.
Shipping MySQL with your Java application is not so easy. Are you sure you need MySQL, and you cannot use some simpler alternatives, like sqlite? If you choose sqlite, there are some 100% java solutions, and that means no difference between platforms and easy deploy of your application.
Think about it, listening to this simple advice can make you save 14-15 hours of work and debugging (with always some possibility of failure, because complex installers do fail).

jar file to use rsync for uploading and downloading files on linux server

I am writing an application for taking backup of user data. In my application i have to perform incremental backup operation. for performing incremental backup i am trying to use rsync algorithm.
i am writing application for windows version. I am having linux hosting server for storing files. after some search i got two lib "jarsync-0.1" and "sisyphe-0.92" but "jarsync-0.1" is
a beta release jar and not giving appropriate result. and "sisyphe-0.92" is configued with linux o.s.
Please could you suggest me any lib file in java that can be used for rsync (for windows version).
There is currently no complete Java port of rsync.
In your case I would look into a native rsync you could call. DeltaCopy has an rsync GUI for Windows which may be useful.. http://www.aboutmyip.com/AboutMyXApp/DeltaCopy.jsp
I guess, this is going to remain unanswered. Since there are no rsync libraries for Java being actively developed.
I just found there is already a discussion on this topic here.
Apparently, most of the guys happen to have implemented rsync via system calls(i.e invoke native calls via your Java app).
The only way I see this application can be coded is via native rsync calls to the OS. Since there are no actively developed java implementations available for rsync. So my suggestion would be to download rsync utility which works in command line and call the utility via process calls in your code.
This is the best possible solution I could think of as of now.

Categories

Resources