Package Java 1.7+ Only Classes with JAR 1.6

127 Views Asked by At

I have an issue where I want to copy Files from one directory to another directory.

Path source = Paths.get(files[i].getPath());
Path target = Paths.get(dir.getName() + "/" + files[i].getName());
try {
    Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    files[i] = target.toFile();
} catch (IOException e) {
    throw new IOException("\t\tUnable to move " + files[i].getName(), e);
}

This code does work, however I am running JRE 1.8, while the system on which I want to run it is running JRE 1.6. Both Path and Files were only available since JRE 1.7.

Is there a way I can use eclipse or the java compiler to package these classes with my jar file.

1

There are 1 best solutions below

1
On BEST ANSWER

Normally you can use -target option to generate class files that target a specified version of the VM. ex: javac -target 1.6. Look here for more options. But this doesn't work when there are classes in the source code which are not available to the target you specify. I believe you use java File IO api and minimum target you can use is 1.7.

If you feel comfortable with the command line, you can try to run OS commands in your Java code. In my opinion it is much easier than trying to rewrite the File and Path java classes. At least that's what I did.

You can find good examples here if you don't know how to execute OS commands in your Java code.