1

I am a relatively novice programmer who can code in HTML, Python, and Java, and I decided to challenged myself to write a block of Java code(using JCreator). I came up with the following challenge:

Design a program in which it would take a .exe file, copy it to a folder (which could be easily changed though a variable), run the file, then delete the file, then continue to do the same process throughout every sub-folder in that directory

I proceeded to write the program, and got the parts of the program to run the exe and to find all of the sub-directories working (with help from stack-overflow, of course). I put all of them together, in addition to the part to copy the .exe file using Files.copy. Every time I run the program, it returns an error.

Part of my code to copy the file, run it, then delete it. I use variables such as "location" and "destination" to allow for easy expansion to the program, and these variables are defined earlier in the program

   public static void EXETestGo(final File folder){

    String destination = folder.getAbsolutePath();  //place to move the exe file; changes each time that the method is called
    String location = "C:/file.exe";                //origin of the exe file
    String runLocation = destination + "/" + "file.exe";    //place of the moved exe file

|

    try{

        Files.copy(location, destination, REPLACE_EXISTING);

        Runtime r = Runtime.getRuntime();       //These two combined
        Process p = r.exec(runLocation);        //'runs' the exe file

        deleteIfExists(runLocation);            //deleates file

    }catch(IOException ex){
        //catches the failed process if it fails

        System.out.println (ex.toString());     //prints out the problem
        System.out.println("Error 404");

    }

Both of these pieces of code are from/used in the same method, so there should not be any problems regarding local variables and whatnot

Any feedback/help would be greatly appreciated!

EDIT: After being run, it returns the "cannot find symbol" error

EDIT #2: I realize that I did not include my imports, which my be the source of the problem My imports:

import java.io.File;
import static java.nio.file.StandardCopyOption.*;
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;
TuckJohn
  • 55
  • 2
  • 9
  • What error does it return? – shridharama Dec 08 '14 at 16:38
  • Error: cannot find symbol – TuckJohn Dec 08 '14 at 21:07
  • Also, it refers to the following 3 lines: the line with Files.copy and deleteIfExists – TuckJohn Dec 08 '14 at 22:12
  • Then it is most likely a syntax error - check [here](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) for more possible root causes. – shridharama Dec 09 '14 at 03:29
  • Also, isn't the directory separator in Windows a backward slash and not a forward slash? – shridharama Dec 09 '14 at 03:30
  • shridharama, according to my other friends who program, It is a problem with 'calling the Files method', but thanks a lot for the link. And yes, as the backslash is reserved as an escape character – TuckJohn Dec 09 '14 at 20:42
  • And yes it is a, but the backslash is reserved as an escape character in Java, and Java uses the forward slash instead – TuckJohn Dec 09 '14 at 20:49
  • The File class has a PathSeparator property that you can use instead of hardcoding / or \ to help make your program more platform independent. – Duston Dec 09 '14 at 21:14
  • >>After being run, it returns the "cannot find symbol" error<< Normally this error occurs at compile time. Please post your entire code or, if it is too big, a snippet that's producing the error. – Gren Dec 09 '14 at 22:14
  • @Joachim, What is posted is intended to be the snipit. If you would like to see another part of the code(such as my imports) please request so. – TuckJohn Dec 10 '14 at 00:16
  • I am sorry for not telling the reasons of my request. 'Unresolved symbol' errors are mostly caused by missing imports or missing declarations, so I wanted to see the whole file.I have found the errors. I post the code in an answer. – Gren Dec 10 '14 at 07:34

1 Answers1

1

There were a missing import statement, String arguments instead of Path arguments and a missing 'static call'
I have commented the corrections in the code.

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.File;
import java.io.IOException;
//Correction: missing import of Files
import java.nio.file.Files;
import java.nio.file.Paths;

public class Test {

    public static void EXETestGo(final File folder){

        String destination = folder.getAbsolutePath();  //place to move the exe file; changes each time that the method is called
        String location = "C:/file.exe";                //origin of the exe file
        String runLocation = destination + "/" + "file.exe";    //place of      


        try{
            //Correction: Files - missing import and the arguments must be of type Path, not String
            Files.copy(Paths.get(location), Paths.get(destination), REPLACE_EXISTING);

            Runtime r = Runtime.getRuntime();       //These two combined
            Process p = r.exec(runLocation);        //'runs' the exe file
            //Correction: 'Files.' missing and argument must be of type Path, not String
            Files.deleteIfExists(Paths.get(runLocation));            //deleates file

        }catch(IOException ex){
            //catches the failed process if it fails

            System.out.println (ex.toString());     //prints out the problem
            System.out.println("Error 404");

        }
    }
}

And here is an example how to walk through a path:

private static void listDirectoryAndFiles() throws IOException {
    final Path root = Paths.get("C:/Test");
    Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class),
            Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir,
                        BasicFileAttributes attrs) throws IOException {
                    System.out.println(dir.toString());
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {
                    System.out.println(file.toString());
                    return FileVisitResult.CONTINUE;
                }
            });
}
Gren
  • 1,850
  • 1
  • 11
  • 16
  • This worked great, Thanks! Although, what would i call it with to search the "C:" drive? – TuckJohn Dec 10 '14 at 18:30
  • I will add an example to my answer how to walk through a path, but because I should not do your homework :), it is a general example and it is up to you, to adopt it for your work. – Gren Dec 10 '14 at 19:00
  • This is not for any kind of HW, and was self-pressured and assigned. And thank-you for all of your help! – TuckJohn Dec 11 '14 at 03:22