简体   繁体   English

从另一个包导入类

[英]Importing Classes from another package

Hello so I'm trying to Import a class from another project I made but I cant get it to work.. this is my code: 您好,我正在尝试从我制作的另一个项目中导入一个类,但我无法让它工作..这是我的代码:

import program.GUI;

public class name {
//code
}

I get the following error 我收到以下错误

Opens the new class wizard to create the type. 打开新类向导以创建类型。

package: program
public class GUI {
}

I have created a prog02>src>program>GUI.java 我创建了一个prog02> src> program> GUI.java

How can I solve it. 我该怎么解决呢

I think I see the problem here... 我想我在这里看到了问题......

I don't know if your running java from cmd, but my explanation is going to assume you are. 我不知道你是否从cmd运行java,但我的解释是假设你是。 Outside of using IDEs like Netbeans and Eclipse that pretty much streamline the build process of java code, it's good to know how it works. 除了使用像Netbeans和Eclipse这样的IDE之外,它几乎简化了java代码的构建过程,所以知道它是如何工作的很好。

Ok, here goes: 好的,这里是:

1) Compile your java source code files into bytecode (.class files) by invoking JAVAC 1)通过调用JAVAC将java源代码文件编译为字节码(.class文件)

javac [optional flags] [path to file intended for compilation]

This creates the bytecode that the JVM will need in order to execute. 这将创建JVM为执行而需要的字节码。

2) Invoke the Java interpreter (JVM) to run your bytecode. 2)调用Java解释器(JVM)来运行字节码。

java [optional flags] [name of class file w/o .class extension]

If everything goes right this command will create a JVM process with main.java being the main entry to the program that creates the initial thread that runs your program. 如果一切顺利,此命令将创建一个JVM进程,其中main.java是程序的主要条目,它创建运行程序的初始线程。

Here what you should write to get you program to compile with you package dependencies. 在这里你应该编写什么来让程序与你的包依赖项一起编译。

  • cd to base dir that contains program with main method ( src ) cd到包含带有main方法的程序的基础目录( src
  • javac -cp . program/ javac -cp . program/ (check by going to dir to see if a GUI.class file was generated) javac -cp . program/ (通过转到dir来查看是否生成了GUI.class文件)
  • javac -cp . Main
  • java -cp . Main

That should do it. 应该这样做。 Otherwise from and IDE standpoint you either don't have file in the right directory or you are not using the right syntax for specifying your package and import. 否则,从IDE的角度来看,您要么没有正确目录中的文件,要么您没有使用正确的语法来指定包和导入。

The package declaration on the GUI class is not right. GUI类上的包声明是不对的。 It should be: 它应该是:

package program;

public class GUI {
}

You may also find this useful: Java Code Conventions 您可能还会发现这很有用: Java代码约定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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