call from java Main to methods in another java file

不羁岁月 提交于 2021-02-10 15:53:58

问题


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();
   }
}

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...


回答1:


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.

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


来源:https://stackoverflow.com/questions/61375532/call-from-java-main-to-methods-in-another-java-file

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