从 java Main 调用另一个 java 文件中的方法 [英] call from java Main to methods in another java file
问题描述
执行时出现错误:在 MyCalcs.MtgeCalc() 行中找不到符号;在文件 Main.java 中为什么会这样???在文件 Main.java 我有:-
When executing I have error: cannot find symbol in the line MyCalcs.MtgeCalc(); in file Main.java Why is it so??? in file Main.java I have:-
e/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.MyJava.002mavern;
public class Main {
public static void main(String[] args) {
MyCalcs.MtgeCalc();
}
}
在文件 MyCalcs.java 中我有:-
and in file MyCalcs.java I have:-
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.MyJava.002mavern;
public class MyCalcs {
public MyCalcs() {
}
public static double MtgeCalc(){
System.out.println("mtgecalc");
return 10;
}
}
...只是在此处添加更多文本以满足发布问题数据完整性检查,但似乎此处需要大量文本,因此我将继续输入直到我能够发布我的问题...
...just adding more text here to satisfy the post question data integrity checks but it seems a lot of text is needed here so i'll keep typing until I am able to post my question...
推荐答案
您是否尝试过更改包装名称?正如 user
所提到的,包名不能以数字开头.尝试 com.MyJava
,而不是 com.MyJava.002mavern
,代码将编译并运行.
Have you tried changing the packing name? As user
mentioned, package name cannot start with a number. Try com.MyJava
, instead of com.MyJava.002mavern
, and the code will compile and run.
更新 #2:除了错误的包,似乎并不是所有的 Java 文件都被编译.尝试 javac *.java
来编译两个 Java 文件.此外,有关 javac
和 java
命令的更多信息,请参阅以下 2 个 Java 8 参考:
UPDATE #2: Besides the incorrect package, it seems not all the Java files are compiled. Try javac *.java
to compile both Java files. Also, see these 2 Java 8 references for more information on the javac
and java
commands:
- javac:https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html
- java:https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html
这篇关于从 java Main 调用另一个 java 文件中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!