Compiler does not find other classes in the same directory

筅森魡賤 提交于 2019-12-06 10:53:30

问题


I wrote a Java program that has 3 classes. When, I use javac, I am getting errors whenever my main class attempts to interact with the other classes. Is there anything special that I need to do? I am just calling javac Main.java. Any help would be greatly appreciated.

Edit:

DFA myDFA = new DFA();
String test = args[0];
if(myDFA.accept(test))

and the error is:

Main.java:19: cannot find symbol
symbol: class DFA
location class dfa.Main

I have 3 of those errors


回答1:


Yes, you need to specify the classpath using the -classpath option on javac when you compile.

Try compiling like this:

javac -classpath . *.java

Note the 'dot' after -classpath. It tells the compiler to look in the current directory to find any .java files that it needs.

If you need other paths or JARs, you have to make sure that they appear in the -classpath as well.




回答2:


You need to to compile the classes indivdually i.e. javac class1.java javac class2.java javac class2.java

etc.

and then execute as

java cp . MainClass.Main




回答3:


first, use an IDE. don't do cmd line.

if you use javac, you should give it all source files that should be compiled

javac Main.java DFA.java ... 

javac *.java

javac -sourcepath .  Main.java 

again, get an IDE, don't do cmd line.



来源:https://stackoverflow.com/questions/3653490/compiler-does-not-find-other-classes-in-the-same-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!