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

查看:121
本文介绍了导入自定义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:

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

所有3个文件都在同一目录中 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

两个班级都应该从以下开始:

Both classes should start with:

 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/VaporVisiotr.java

这将立即编译两个文件,并将其编译的类文件放在项目/类中:

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文件和类路径中的classes目录。主类的完全不受欢迎的名称是 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 quelidied 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,并将其类文件放在项目中类。然后你需要编译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天全站免登陆