My Environment is OSX BIG SUR.
For space reasons, I need to move files from my Hard disk to a NAS (on my local network).
The current applications accessing these files (en read mode) were developed in Java.
I suppose I should use a Mount command in programs with the SMB protocol, but how to do this without be prompted for password with the SUDO command ?
So I'm looking for some examples.
You have a few ways to go:
Have java execute the smb mount command
This is a walking disaster; you don't want your java process to gain root privileges to do this.
Have java be an smb client
Possible... use JCifs-ng. The docs are lacking, and this would have been a lot simpler if it had integrated into the new files / filesystem API, but it doesn't. You'll have some coding to do.
Learn sysops
All you really need is for the OS to have that samba share mounted someplace and then, to your java process, it's all just files on a filesystem. Don't make the smb mount happen in java, ensure it's already set up before your java process even starts.
Related
An executable jar is distributed to users per network drive. I want to prevent users from running it directly on the network because it causes performance and other problems.
One solution can be to check the execution path and compare it with the network drive path, but what if the network drive path is not known in advance? Is there any other possibility?
I don't think you can do this in pure Java. I'm pretty sure you will need to either run an external command or make a call into native code to detect the whether the JAR file is on a network share.
The first part you need to do is to obtain the absolute path for the JAR file:
Class mainClass = MyMainClass.class;
File jarFile = new File(mainClass.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI());
I found a "gist" on github written by Aaron Digulla that does the second rest of the task:
https://gist.github.com/digulla/31eed31c7ead29ffc7a30aaf87131def
Aaron's code takes a Windows absolute path, extracts the drive letter and then runs the Windows net use <drive-letter>: command to test whether the drive is a share drive.
An alternative would be to implement the logic a batch file does the following:
Extract the drive name for the JAR file
Use net use ... to test for a share drive
Use the same technique to check that the Java installation is not on a share drive either1.
Use the java command to start the application
Finally, is probably a bad idea to refuse to run on a share drive. The user may have reasons to do this that you are not aware of. I would suggest prompting the user and giving the user the option of continuing anyway. Or something like that.
Alternatively, document in the application's installation instructions that it is inadvisable to install the JRE / JDK and the application on a share drive (or equivalent).
1 - A Java install on a share drive will give as bad if not worse performance than an application JAR on a share drive.
I've used Commons IO to write a program that copy files and other things. But I want to copy a file to a local ip address \\10.x.x.x, but the user doesn't have rights to do the copy, so I need to put an ID and password to access it. However I cannot find a way I can do that.
To move file I use :
FileUtils.moveFileToDirectory(fichier, destDir,true);
But my directory is something like \\10.x.x.x\files and only a few users can write in that directory so I have an ID & password that let you move files there. I want that even if the users don't have rights to move files to that directory my program can do it.
It is not really the way Windows security works. If you really want to do it that way, you will have to use Java Native Interface or Java Native Access, and manage to call the WNetAddConnection function from Mpr.dll (and do not forget to call WNetCancelConnection when done).
But you would have to store a password in your program, which is poor security practice.
The standard way to do that would be to start a service that would run under a user that has access to the desired directory, and have your program to communicate with it using whatever you want, the simplest way being probably TCP/IP. But unless you have special requirement for that I would not recommend to use Jave for those kinds of program.
A more Java alternative would be to start a Tomcat service on server machine running under a user having access to the directory. That way you just have to develop a standard Java Web Application able to upload files that would save the files to the proper directory. But it would be a traditionnal and portable Java application with no need for JNI nor JNA.
If cannot use a Tomcat and do not want to invest to much on it, you could split the program in pieces :
one client program that copies files on a directory (on server machine) with File creation rights for everybody - can decays to the copy utility if nothing more has to be done or can easily written in Java
one server program that will run on server machine under a user that has full write permissions on target directory. This one can also be easily written in Java
you can easily install the server program as a service on the server machine with sc and srvany according to this answer on ServerFault
If you use a client program, you could easily add a digital signature file with each copied file, but as I said above, it is poor security practice and add little if any security. At least the program should be executable and not readable, and the sources should be kept hidden. It is better to log the users that copied the file and ask them what happened is you find a problem.
I want to copy a file in local windows machine to another windows machine in the same LAN or Intranet using JAVA. I browsed and found that we can copy file using the copy command of ms-dos but it copies only to the shared folder of remote machine.
I don't want to copy to the shared folder but instead I want to copy to a folder inside C: drive which is not a shared one.
I tried using pscp command. It works for windows to linux file copying but not for windows to windows file copying.
The best way is to send the File with an selfwritten socket application.
You cant copy a non shared folder. But u can write a server client application to send the data over the network:
http://www.rgagnon.com/javadetails/java-0542.html
As far as i know, there are several ways to achieve it.
Provide two agents on the machine A and machine B, then you can copy the files to each machine by your own implement APIs.
Use the winrm protocol like chef does, more detail please refer to the below link.
You can also refer to some opensource project.
https://github.com/cloudsoft/winrm4j
https://github.com/OpenNMS/wsman
You cannot copy to a folder that it is not shared.
The operating system of the computer where you want to copy files will never allow you to write wherever you want for obvious security reasons.
I need to move few files in a directory to another directroy in another machine. The source and destination directory names are known in advance. These directories will be in different Operating System(i.e. the source can be Linux and Destination can be in Windows).
To solve this, i wrote a Socket program in Java that successfully copies files from source directory to a destination directory(Tested in Windows OS). But this application needs program running in both the host and remote machine. So, my question is :
1) Is there any way to transfer files(in the ways described above) using Java Socket program without having a program running in the remote machine?
2) Whether file transfer using Java socket program, bypass firewal. In other words, if a firewall is present in the remote machine will that stop the transfer process?
3) Any alternative approach to transfer files(in the ways described above) using Java where only the host or remote need to run.?
Thanks in advance,
nks
1) You always need a program running on both machines. However, a way to get around this problem is to use a program already running on one of these machines.
e.g. If you make the directory a Windows share (assuming this is already running) you can mount this share on the Linux box (using smbfs), you can then copy files using cp This means you don't need Java on either end, but you need A program and A service (but it can be an existing one)
2) There isn't a way to by-pass a firewall unless your network is seriously mis-configured. This is the whole point of having one.
3) Once you mount either the Windows box on Linux, or the Linux files (using Samba) on Windows youc an copy files easily, even using Java. ;)
I'm currently doing a project that has to be run on a windows machine. The application creates a CSV file and saves it to the windows filesystem. I would like a way to transfer this file to a machine running linux into a directory selected by the user.
I am not asking for code (although if it helps then feel free :P ) but asking more for what protocols to use (ftp etc) and wether I need to take anything into account such as permissions in Linux as I am fairly in experienced with programming for linux file systems.
Thanks in advance :)
Can the windows machine see the linux filesystem? Is there a samba server running on the linux machine? If so, you can simply copy it from one filesystem to the other.
If not, you can try SCP or SFTP but you need to have the keys setup. Another way is to use Runtime.exec to copy the file using a windows specific command utility.
If all else fails, run a java process on the linux server and connect to it using sockets. Your windows client can write the file to the socket and it can be written out by the linux server.
I would try using SSH as SCP is a fairly standard and convenient way to transfer files securely. In Java you can use jssh amoungst other libraries.
If you don't want to do it programatically, I'd suggest using Rsync. It should be available for most *nix distros and you've got a Windows version called DeltaCopy that implements the same protocol.