I want to inherit my own java file to the java file that I compile in Java's Compiler Tools, but I can't find the symbol.

Asked 2 years ago, Updated 2 years ago, 38 views

There is a java file that I want to compile using javax.tools, but it says that I can't find a symbol because I import another self-made java file.
(aaa.java wants to extend bbb.java, but generates an error when compiling aaa.java)
There are so many files I want to import that I cannot write them directly to the files I want to compile.Is there any way?

Please forgive me for my lack of programming experience.

Excuse me.
Here is the code to compile.

public class DoCompile{
    public DoCompile(){
        File f=new File("D:/Desktop/compiletest/aaa.java");
        File = new File("D:/Desktop/compiletest/classes");

        String [ ] args = {
            "-d", d.getAbsolutePath(),
            f.getAbsolutePath()
        };

        JavaCompiler c=ToolProvider.getSystemJavaCompiler();
        intr = c.run(null, null, null, args);
        System.out.println("return"+r);
        if(r==0)
            System.out.println("BUILD SUCCESSFUL");
    }
}

aaa.java is a file that inherits a completely different java file, bbb.java

Development Environment (IDE): Eclipse Language Java 7

java

2022-09-30 17:57

2 Answers

The following two points are required.

-sourcepath is an option to specify where to find the source of the class file (in this case, bbb.java) that is not sufficient to compile.This option exists in javac documentation and help.

By using this, the code on the side of the compilation will look like this:Also, there are some simplified parts of the code written during the question, but please read them accordingly.

public class DoCompile{
    public static void main(String[]unused) {
        String [ ] args = {
            "-d", "./classes",
            "-sourcepath", "./src",
            "./src/aaa.java"
        };
        JavaCompiler c=ToolProvider.getSystemJavaCompiler();
        intr = c.run(null, null, null, args);
        if(r!=0){
            System.out.printf("build failed:%d\n",r);
        }
    }
}

The actual sample code you can try is https://github.com/koron/ja-stackoverflow-22453.The name of the file to compile has changed a little, but I think the basic structure is the same.For your information.


2022-09-30 17:57

I guess I can't find the symbol because I haven't been able to import the required class.

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

If you add this, I think the compilation will go through


2022-09-30 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.