I need to move any file matching a pattern defined by an rsync file pattern (used for --include, --exclude).
For example: *.str
I need to move any file in /source/ to /archive/ locally using Java. Will a simple File.renameTo method work? I don't see how looking at the source code.
What's the best way to do this? Any recommend libraries?
Background: some files and directories are being rsynced to multiple hosts. After it is successfully rsynced to each host, I need to archive the file (move it to the archive dir) locally. It works fine if local/source dir is a file, but when it's a directory and the rsync option --include is given, ONLY those files need to be moved after rsync is successful.
You can use the ant move task programmatically. Get ant and use org.apache.tools.ant.taskdefs.Move
Related
I'm making a file import system, and I can't move files into the compiled .jar file the application is in.
Here's what I'm trying to do:
Path FROM = Paths.get(filePath.getText());
Path TO = Paths.get("C:\\Users\\" + System.getProperty("user.name") +
"\\AppData\\Roaming\\.minecraft\\mods\\music_crafter-1.0\\src\\main\\resources\\assets\\music_crafter\\sounds\\block\\music_player");
//jar file
Files.move(FROM, TO.resolve(FROM.getFileName()), StandardCopyOption.REPLACE_EXISTING);
You need to handle the jar file internally. A Jar is not a directory, it is a compressed container file (pretty much a ZIP file with a different extension).
To do this, given that you are on Java 6, you have 2 options:
Unzip the contents to a temporary working directory (there are built
in APIs for this, or use a library such as Apache Commons Compress)
do your work (copying, deleting, etc) and then re-zip.
Make external command line calls to the Jar utilities that come with
Java
Of those, only (1) makes any real sense.
A third option would be available if you could up your Java to 7+ which would be:
3. Use a Zip File System Provider to to treat it as a file system in code
All that said, however:
As per comments on your question, you really might want to look at if this something you need to do at all? Why do you need to insert into existing jars? If this is 'external' data, it would be much better in a separate resource location/container, not the application jar.
I am writing a program in java with netbeans IDE which receives a jasper report *.jrxml and then displays the report for the user. I wrote the following line of code for the file path
String reportSource = "src\\jasper-reports\\report.jrxml";
but when I move the dist folder in some other place and try to run the jar file inside it, my program can not find the report.
my problem is that were should I put the *.jrxml file and how to define it's path in my program so that when I want to give my software to someone else it runs without any errors (e.g. the program can find the file)
avoid using absolute paths. try to include the file as a resource in your netbeans project. then in your code you can search for and load the file as
new InputStreamReader((Main.class.getResourceAsStream("/report.jrxml")))
something like that depending on where the file resides in your project
it's more recommended using one of the two approaches:
pass the locations/paths as a -Dproperty=value in the Java application launcher command line http://www.tutorialspoint.com/unix_commands/java.htm
store it the locations/paths in a configurations file with a unique key and edit the file accordingly for different environments,
e.g.this files always stored in ${HOME}/config_files/ directory
absolute paths considered a bad practice
How do I get the location of the executed jar? I found a lot of solutions but none of them work for me. When I run them in my IDE everything is fine. As soon as I build a jar file and run it with java -jar myapp.jar the output is like /.../myapp.jar!/foo/bar
I will run the code in myapp.jar - not in any library.
Location of jar: /home/me/dev/myapp/myapp.jar
Expected output: /home/me/dev/myapp/
I don't want the working directory as I would get with System.getProperty("user.dir");
Why I want to do this:
I want to store and load a file beside the actual jar. Like
/home/me/bin/myapp/myapp.jar
/home/me/bin/myapp/license.key
I want to avoid storing the file into some generic folder like System.getProperty("user.home");. And I don't want to store the file within the jar file.
java.nio.file.Paths.get(".").toAbsolutePath() will return absolute path to current directory.
I use something along these lines:
[YourClass].class.getProtectionDomain().getCodeSource().getLocation().getPath()
Regards
I need to move the files from one HDFS directory to another HDFS directory.
I wanted to check if there's some easier way (some HDFS API) to achieve the same task, other than InputStream/OutputStream ?
I've heard of FileSystem.rename(srcDir, destDir); but is unsure if this will delete the original src directory.
I don't want to remove the original directory structure, only move the files from one folder to another directory.
e.g
input Dir - /testHDFS/input/*.txt
dest Dir - /testHDFS/destination
After moving the files, directory should look something like this :-
input Dir - /testHDFS/input
dest Dir - /testHDFS/destination/*.txt
PS : I want to achieve this working inside mapper function for each file.
Any help would be appreciated.
FileSystem.rename will move the file from source to destination directory. I believe you can use it for your requirement.
The best way to do this is with org.apache.hadoop.fs.FileUtil.copy(), setting the deleteSource parameter to true. People commonly use FileSystem.rename(), but that function will fail silently for invisible issues (such as the source and destination Paths being on different volumes)
You can use DistCp programmatically verify this
my problem is that I want to add some .class files from a normal directory into an .jar file. Do I have to extract it before or can I add the files "on the fly"?
Although I agree with Jordan and suggest using the system command, that was not an option for the team I work on.
If you have to use his second solution, it is extremely important to note that Zip::ZipOutputStream will override any existing jars; i.e., you won't be adding to an existing jar, you will be creating a new one. This code will add a file to an existing jar:
require 'zip/zip'
Zip::ZipFile::open 'path/to/jar' do |jar|
jar.add 'filename_in_jar', 'path/to/file/you/want/to/add'
end
If it was me I would almost certainly just call the jar command within Ruby to do this:
system 'jar uf jar_file.jar input_file(s).class'
# or
%x[ 'jar uf jar_file.jar input_file(s).class' ]
Reference here.
If you still want to do this without calling jar you should be able to do it with rubyzip, since JAR files are just ZIP files with a particular structure. Something like this:
require 'zip/zip'
filename = 'class_file.class'
Zip::ZipOutputStream::open "jar_file.jar" do |zip|
zip.put_next_entry 'dest/path/in/jar/' + filename # don't forget the path
File.open filename, 'rb' {|f| zip.write f.read }
end
There are also a few Ruby wrappers for libarchive that could do this. E.g.