导入自定义 Java 类 [英] Import custom Java class

查看:32
本文介绍了导入自定义 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,我在网上四处寻找解决方案,但似乎都没有.请帮帮我.

I'm new to Java and I've looked around the web for solutions but none seem to work. Please help me.

我有两个文件.其中之一是包含 main 函数的 java 文件.其中:

I have two files. One of them is the java file that contains the main function. In it:

...
VaporVisitor visitor = new VaporVisitor();
...

使用该命令,我想创建一个新对象 VaporVisitor,它是名为 VaporVisitor.java 的单独文件中的一个类.但是 Java 不识别 VaporVisitor 是什么,大概是因为它不知道 VaporVisitor.java 存在(它在同一目录中).我试着让它们成为同一个包的一部分,在不同的包中并导入......但它们都失败了.任何人都可以给我任何指导吗?

With that command, I want to create a new object VaporVisitor, which is a class in a separate file called VaporVisitor.java. However Java doesn't recognize what VaporVisitor is, presumably because it doesn't know VaporVisitor.java exists (it's in the same directory). I tried making them part of the same package, in different packages and importing...and all of them failed. Can anyone give me any guidance?

谢谢!

这正是我正在做的事情,以及我收到的错误消息:所以我有3个文件.V2VM(带有我的主要功能)、VaporVisitor 和提供的具有多个自定义类的 jar 文件.jar 文件没有给我任何问题;它试图让 java 识别 VaporVisitor.

Here's exactly what I'm doing, and the error message I get: So I have 3 files. V2VM (with my main function), VaporVisitor, and a provided jar file that has several custom classes. The jar file isn't giving me any problems; it's trying to get java to recognize VaporVisitor.

所以在 V2VM(主要功能)中,我尝试放入:import V2VM.java;,但没有用.我尝试将 V2VM 放在名为 vv 的子文件夹中,将 package vv; 添加到 VaporVisitor 并放入 V2VM.java import vv.* 但这也不起作用.

So in V2VM (the main function), I've tried putting in: import V2VM.java; which didn't work. I've tried putting V2VM in a subfolder called vv, added package vv; to VaporVisitor and put in V2VM.java import vv.* but that didn't work either.

为了编译,我尝试了 javac -classpath [jar 文件].jar V2VM.java

它给我的错误:

V2VM.java:15: cannot find symbol
symbol  : class VaporVisitor
location: class V2VM
VaporVisitor visitor = new VaporVisitor();
^
V2VM.java:15: cannot find symbol
symbol  : class VaporVisitor
location: class V2VM
VaporVisitor visitor = new VaporVisitor();
                           ^

当我运行 javacc 时,我与 V2VM 位于同一目录中,这也是其他两个文件所在的位置.我试过将 V2VM 和 VaporVisitor 放在同一个包中,但这也不起作用.所以他们现在不是任何包的一部分......

When I run javacc I am in the same directory as V2VM, which is also where the other two files are located. I've tried putting V2VM and VaporVisitor in the same package, but that didn't work either. So they are not part of any package now...

编辑 2:VaporVisitor 和 V2VM 的源代码

EDIT 2: SOURCE CODE OF VaporVisitor and V2VM

V2VM.java:

package vv; //whether I put this or not, it doesn't work

//this stuff was provided, and is related to importing contents of the jar file; don't think this is the problem
import cs132.util.ProblemException;
import cs132.vapor.parser.VaporParser;
import cs132.vapor.ast.VaporProgram;
import cs132.vapor.ast.VBuiltIn.Op;

import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;

import vv.VaporVisitor;  //whether I put this or not, it doesn't work

public class V2VM{

public static void main(String [] args){

VaporProgram vp = parseVapor(System.in, System.err);
VaporVisitor visitor = new VaporVisitor();

for(int i=0; i<vp.functions.length; i++){
for(int j=0; j<vp.functions[i].body.length; j++){
    vp.functions[i].body[j].accept(parameter, visitor);
    }
}
}

public static VaporProgram parseVapor(InputStream in, PrintStream err){
  ...
}

对于 VaporVisitor.java:

For VaporVisitor.java:

package vv;

public class VaporVisitor extends VInstr.VisitorPR<Parameter_Type, Return_Type, RuntimeException>{
   ....
}

三个文件都在同一个目录下vv

All 3 files are in the same directory vv

推荐答案

好的.所以首先,在 Java 中永远不要通过将 .java 附加到它的名称来引用它.第二:为了编译使用另一个类 B 的类 A,必须编译类 B,并在类路径中可用.或者你必须同时编译 A 和 B.

OK. So first of all, you never refer to a class in Java by appending .java to its name. Second: in order to compile a class A that uses another class B, the class B must be compiled, and available in the classpath. Or you must compile both A and B at the same time.

因此,您应该具有以下结构:

So, you should have the following structure:

project
   lib
      someFile.jar
   src
      vv
         V2VM.java
         VaporVisitor.java
   classes

两个类都应该以:

 package vv;

无需在 VaporVisitor 中导入 V2VM,反之亦然,因为它们在同一个包中.

There's no need to import V2VM in VaporVisitor or vice versa, since they are in the same package.

要编译文件,您应该在项目目录中,并使用以下命令:

To compile the files, you should be in the project directory, and use the following command:

javac -cp lib/someFile.jar -d classes src/vv/V2VM.java src/vv/VaporVisitor.java

这将同时编译这两个文件,并将它们编译的类文件放在 project/classes 中:

This will compile the two files at once, and put their compiled class files in project/classes:

project
   classes
      vv
         V2VM.class
         VaporVisitor.class

您将 jar 文件放在类路径中,因为您编译的类使用此 jar 文件中的类.

You put the jar file in the classpath because the classes you compile use classes from this jar file.

然后,要运行您的类,您需要同时拥有 jar 文件和类路径中的类目录.而主类的全限定名是vv.V2VM.所以命令是

Then, to run your class, you need to have both the jar file, and the classes directory in the classpath. And the fully qualified name of the main class is vv.V2VM. So the command is

java -cp lib/someFile.jar:classes vv.V2VM

如果你在 windows 上,你必须使用 而不是 /,以及 ; 而不是 :.

If you're on windows, you must use instead of /, and ; instead of :.

如果你想先编译VaporVisiotr,然后再编译V2VM,你可以.但是,您必须执行以下操作:

If you wanted to compile VaporVisiotr first, and then V2VM, you could. But then you would have to do the following:

javac -cp lib/someFile.jar -d classes src/vv/VaporVisitor.java

这仅编译 VaporVisiotr,并将其类文件放在 project/classes 中.然后需要编译V2VM,依赖VaporVisitor.所以编译的 VaporVisitor 类必须在类路径中可用.所以命令是

This compiles VaporVisiotr only, and put its class file in project/classes. Then you need to compile V2VM, which depends on VaporVisitor. So the compiled VaporVisitor class must be available in the classpath. So the command is

javac -cp lib/someFile.jar:classes -d classes src/vv/V2VM.java

如果您决定将 VaporVisitor 放在另一个包中(例如 vv.foo),那么您将需要以下结构:

If you decided to put VaporVisitor in another package (vv.foo for example), then you would need the following structure:

project
   lib
      someFile.jar
   src
      vv
         V2VM.java
         foo
             VaporVisitor.java
   classes

VaporVisitor.java 需要以

The VaporVisitor.java would need to start with

package vv.foo;

V2VM.java 文件需要有

And the V2VM.java file would need to have

package vv;
import vv.foo.VaporVisitor;

阅读关于包的教程.

这篇关于导入自定义 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆