Skip to content
Advertisement

Unable to locate and load file in Eclipse IDE, File not found

I’m new to Java, and I am facing this issue in Eclipse. Even after pointing it to the correct file, it shows a file not Found Error.

I am trying to compile code from a Java file using the Java Compiler API. The code words fine in Visual Studio with setting everything in root, But gives this error in Eclipse with all these directories.

Also, why are there three different src folders in the image?

My project structure

package com.example.app;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;

public class compilier {
    public static void main(String[] args) throws IOException {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null, null, null, new File("com/example/app/Code.java").getAbsolutePath());

        if (result == 0)
        {
            System.out.println("File Compiled");
        }
            try {
                    String package_dir = "/demo/src/main/java/com/example/app";
                try{
                    ProcessBuilder builder = new ProcessBuilder("java", package_dir.concat("/Code"));
                    builder.redirectErrorStream(true);
                    File outfile = new File((package_dir.concat("/output.txt")));
                    builder.redirectOutput();
                    builder.start();
                    if (outfile.length() > 3000)
                    {
                        System.out.println("Exceeded buffer limit");
                        System.exit(1);
                    }
                } catch(IOException e) {
                    e.printStackTrace();
                }

            } catch (Exception err) {
                System.out.println("Error!");
                err.printStackTrace();
            }
        }
    }

Error Message

Advertisement

Answer

Your path looks wrong. The /demo directory would need to be in the root of your current drive.

Also, the output of a Maven build is found in the target directory. The Java class files are generated there, and the resource files are copied over from src/main/res hierarchy. The .Java files are lost. You could add a Maven task to copy the .Java files but this would be very nonstandard.

Finally you need to load resource files using the classpath. There are lots of examples on the Internet. Otherwise you may end up with a project that finds the file in Eclipse but not when deployed in a .jar or .war file. Happy hunting.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement