简体   繁体   中英

Import custom java file

I'm currently trying to read some source code in Java I found online to study and learn the material. I want to compile the files first to make sure they work before I study it. When I try to compile though, the compiler complains that it can't find some of the files it needs to import. So opening up the main.java, I find

 package br.com.seimos.minijava;

 import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.util.ArrayList;

 import br.com.seimos.minijava.parser.MiniJavaParser;
 import br.com.seimos.minijava.syntaxtree.Program;
 import br.com.seimos.minijava.visitor.TreeDumper;
 import br.com.seimos.minijava.visitor.TreeFormatter;

The errors I'm getting are coming from not being able to find MiniJavaParser , TreeDumper ...the 3rd chunk of code. Those files exist in the same directory as the main file though, so what is going on? What is br.com.seimos.minijava stuff? I tried putting the files in that those folders (as in br\\com\\seimos\\minijava\\PUT_FILES_HERE ) but still no good. Does br need to be in the root directory?

Thanks, I realize this is probably a really elementary question...

For import br.com.seimos.minijava.parser.MiniJavaParser; your MiniJavaParser class must be in the directory br\\com\\seimos\\minijava\\parser\\ and not br\\com\\seimos\\minijava\\ . Similarly for other classes. Try changing it.

You're on the right track. You will need to put those files in br/com/seimos/minijava/... as indicated by the package name of each. Java requires that you put files in a directory hierarchy that matches their package names.

Then, you'll want to compile using a command like:

javac br/com/seimos/minijava/parser/MiniJavaParser.java

This is all a bit inconvenient from the command line, especially for a larger project, so you might be better of getting a Java IDE and having it help you arrange the files.

The required directory structure is br/com/seimos/minijava/OTHER_FOLDERS/SOURCE_FILES.java . For instance, the path to MiniJavaParser should be br/com/seimos/minijava/parser/MiniJavaParser.java . You should then run the Java compiler from the parent directory of br .

If you're using Eclipse or another IDE, you should configure your project settings to handle this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM