How does eclipse execute a Java application?

I have a very simple .java

file in the Eclipse Java Project that creates a .txt

.

package mproject.assgn.q12;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class ColumnPattern {

    public static void main(String[] args) {
        System.out.println(getInputFile().getAbsolutePath());
    }

    private static File getInputFile() {

        String content = "110\t100\t255\t122\n"
                + "100\t109\t180\t111\n"
                + "22\t101\t122\t110\n"
                + "211\t122\t177\t101";

        File file = new File("file2.txt");
        try {

            if(!file.exists()){

            System.out.println("Creating Input File since it does'nt exist");
            file.createNewFile();

            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(content);
            bw.flush();
            bw.close();

            return file;
        }

        catch(IOException e){
            e.printStackTrace();
            System.err.println("Error during File I/O");
        }

        return null;
    }

}

      

I want to know why the generated file is in /projectFolder/

and .java is in /projectName/src

and the class file is in /projectFolder/bin

. Shouldn't you create a text file in the / bin folder where the .class file is located?

How does eclipse execute the .java file when we select " Run As Java Application

"?

Will it compile it into a jar and then execute it?

+3


source to share


2 answers


Why is the text file not in / bin
Eclipse creates default hidden project files - .project. The directory in which this file is created is considered the root directory of the project. When using relative file paths, they are always resolved with the project root.

How Eclipse App Works



Eclipse compiles the classes under / bin (unless otherwise noted) and executes the class using the main

. Considering that you have multiple classes with a main method, you must select after selection Run As Java Application

.

Also, the note about jar files are just archives containing classes and other files. When you execute the jar file, you basically execute the main method of the main class declared in the file META-INF/Manifest.mf

.

+2


source


When you create a file with java.io.File

such a constructor String

, this path refers to the current working directory from which the application is launched. When you choose Run As > Java Application in Eclipse, the default working directory is the root of the project, which explains why your file was created there.



You can easily change the working directory to run your application from Eclipse: open the Run Configs (or Debug Config ) dialog (either using the toolbar buttons for Run / Debug or the Run menu ), select the run configuration for your application, then go to the tab Arguments . At the bottom you will see a section for choosing a working directory.

+2


source







All Articles