如何在Java中使用VSCode将一个文件的类导入到另一个子文件夹中的文件?

huangapple go评论92阅读模式
英文:

How to import class of one file to another in a subfolder in Java using vscode

问题

import mytest.Mypckg;

public class Learn {
  public static void main(String[] args) {
    System.out.println("你好,世界");
    Mypckg.show();
  }
}
package mytest;
public class Mypckg {
  public static void show(){
    System.out.println("早上好");
  }
}

两个文件都在同一个名为 mytest 的子文件夹中,但是当我尝试运行主文件 => Learn 时,我遇到了一个错误:"Learn.java:2: 错误: 找不到符号import mytest.Mypckg;"。我还尝试了从 Learn 文件中删除 "import mytest.Mypckg",因为它们都在同一个包中,但是当我编译时,我会得到一个错误,提示找不到符号 "Mypckg"。

英文:
import mytest.Mypckg;

public class Learn {
  public static void main(String[] args) {
    System.out.println("Hello World");
    Mypckg.show();
  }
}
package mytest;
public class Mypckg {
  public static void show(){
    System.out.println("Good Moring");
  }
}

Both file are in same subfolder name mytest
but when I try to run main file => Learn, I am getting an error
"Learn.java:2: error: cannot find symbolimport mytest.Mypckg;". I have also tried by removing import mytest.Mypckg from Learn file as both are having same package but when I compile I am getting an error as "cannot find symbol Mypckg".

答案1

得分: 4

我假设这两个文件都在名为mytest的包/文件夹中,
而且您的代码似乎缺少以下添加的行:

package mytest;//添加的行
//import mytest.Mypckg;

public class Learn {
  public static void main(String[] args) {
    System.out.println("Hello World");
    Mypckg.show();
  }
}
英文:

I am assuming that both the files are in the package/folder named mytest
and your code seems to miss this line added below

package mytest;//added line
//import mytest.Mypckg;

public class Learn {
  public static void main(String[] args) {
    System.out.println("Hello World");
    Mypckg.show();
  }
}

答案2

得分: 0

你说得对,有一点我没有提到,在主文件(Learn.java)中没有提到package mytest。这是我的错误,但是在我的电脑上,当我在两个文件中都提到了package mytest时,我仍然得到了一个错误,如"无法找到符号import mytest.Mypckg;"

最后我找到了解决方案。实际的问题是我在子文件夹mytest中使用了编译方法。事实上,我尝试通过跳出子文件夹来编译文件,以获取mytest文件夹的路径。但真正的问题在于,我没有在编译代码中使用类路径(即-cp)。因此,在vscode中编译包文件,我们必须进入父文件夹并键入以下代码。

javac -cp . mytest/Learn.java // 带有文件路径的编译器代码

而且,要运行文件,我们必须使用

java mytest.Learn // mytest是一个子文件夹(包文件夹),learn是主文件

在这里,我附上了图片,以更好地帮助我的同事们理解。我希望我的答案对那些想要在vscode中编译带有包的Java代码的人有所帮助。

如何在Java中使用VSCode将一个文件的类导入到另一个子文件夹中的文件?

英文:

you are right in one case that I have not mentioned package mytest in main file (Learn.java). that's my bad but in my computer when I mentioned package mytest in both file then also I was getting an error like "cannot find symbol import mytest.Mypckg;"
如何在Java中使用VSCode将一个文件的类导入到另一个子文件夹中的文件?

Finally I found the solution. The actual problem was I was using compiling method inside the subfolder mytest. Infact I tried to compile the file by going outside the subfolder to get the path for mytest folder. But the main culprit was something else.I was not using the class path (i.e -cp) in my compilation code. So to compile the package file in vscode we have to go to parent folder & type the following code.

javac -cp . mytest/Learn.java // compiler code with file path 

And also to run the file we have to use

java mytest.Learn     // mytest is a subfolder (package folder) and learn is a main file

Here Below I have attached the image for better clarification for my fellow colleagues. I hope my answer will help someone who wants to compile java code with package in vscode
如何在Java中使用VSCode将一个文件的类导入到另一个子文件夹中的文件?

huangapple
  • 本文由 发表于 2020年9月13日 15:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定