Browse Source

Improves File Copying of .class Files

Andreas T. Meyer-Berg 5 years ago
parent
commit
c57a0fe795

+ 20 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -10,6 +10,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.nio.channels.FileChannel;
+import java.nio.file.CopyOption;
+import java.nio.file.Files;
 import java.util.LinkedList;
 
 import javax.tools.JavaCompiler;
@@ -308,6 +310,7 @@ public class ImportController {
 		return importJavaClass(javaFile, new File("bin/main/").getAbsolutePath());
 		}catch(Exception e){
 			System.out.println("Import failed: "+e.getMessage());
+			e.printStackTrace();
 			return null;
 		}
 	}
@@ -353,9 +356,22 @@ public class ImportController {
 		if(destination!=null&&destination.getParent()!=null&&!destination.getParentFile().exists())destination.getParentFile().mkdirs();
 		
 		File source = new File(javaFile.getParent()+"/"+className+".class");
-		//System.out.println("Copy:");
-		//System.out.println("From: " + source.getAbsolutePath());
-		//System.out.println("To:   " + destination.getAbsolutePath());
+		System.out.println("Copy:");
+		System.out.println("From: " + source.getAbsolutePath());
+		System.out.println("To:   " + destination.getAbsolutePath());
+		try {
+			Files.copy(source.toPath(), destination.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
+			try {
+				Thread.sleep(1000);
+			} catch (InterruptedException e) {
+				System.out.println("Sleep Failed");
+				e.printStackTrace();
+			}
+		} catch (IOException e1) {
+			System.out.println("Copying File failed while Importing Class");
+			e1.printStackTrace();
+		}
+		/*
 		FileChannel sourceChannel = null;
 	    FileChannel destChannel = null;
 	    FileInputStream fIn = null;
@@ -374,7 +390,7 @@ public class ImportController {
 	    	sourceChannel.close();
 	    	destChannel.close();
 	    	}catch(Exception e){}
-	    }
+	    }*/
 		//System.out.println(destination.getCanonicalPath()+" exists: "+destination.exists());
 
 		/**