简体   繁体   English

如何在Java中导入没有Eclipse的类?

[英]How to import classes in Java without eclipse?

I'm sorry that this may seem like a basic question, but I need help on importing classes. 抱歉,这似乎是一个基本问题,但是在导入类时需要帮助。 I downloaded the Lightweight Java Gaming Library(LWJGL), but I don't know how to install it. 我下载了轻量级Java游戏库(LWJGL),但我不知道如何安装它。 When I try the test, it doesn't work. 当我尝试测试时,它不起作用。

I have a folder in which I extracted the basic LWJGL files. 我有一个文件夹,在其中提取了基本的LWJGL文件。 I now have a setup like this: 我现在有这样的设置:

generic_folder/libs/lwjgl-2.8.3 generic_folder / libs / lwjgl-2.8.3

I also have this test which I copy-pasted from the tutorial on the LWJGL wiki: It is located in the generic_folder. 我也有此测试,该测试是从LWJGL Wiki上的教程复制粘贴而成的:它位于generic_folder中。 When I try and javac it, it says DisplayExample.java:1: package org.lwjgl does not exist. 当我尝试使用javac时,它说DisplayExample.java:1:包org.lwjgl不存在。

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class DisplayExample {

public void start() {
    try {
    Display.setDisplayMode(new DisplayMode(800,600));
    Display.create();
} catch (LWJGLException e) {
    e.printStackTrace();
    System.exit(0);
}

// init OpenGL here

while (!Display.isCloseRequested()) {

    // render OpenGL here

    Display.update();
}

Display.destroy();
}

public static void main(String[] argv) {
    DisplayExample displayExample = new DisplayExample();
displayExample.start();
}

} }

Do I have to place the files in a certain location or what? 我是否必须将文件放置在某个位置或什么位置? Please help. 请帮忙。 Thanks! 谢谢!

Edit: I am sorry. 编辑:对不起。 I'm new to Java. 我是Java新手。 Please go a little slower. 请慢一点。 Also, LWJGL is a library, so I didn't place any files inside of it. 另外,LWJGL是一个库,因此我没有在其中放置任何文件。

You need to specify a classpath (preferrably with the -cp switch) such that java / javac knows where to find the .jar files you need access to. 您需要指定一个类路径(最好使用-cp开关),以便java / javac知道在哪里可以找到需要访问的.jar文件。 It typically looks as follows: 它通常如下所示:

For compiling: 对于编译:

javac -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-java-files

for running: 运行:

java -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-class-file

(Note that if you're using Windows, I believe : should be replaced by ; .) (请注意,如果您使用的是Windows,我相信:应该用;代替。)

You need to add that jar to your classpath when you invoke javac , like so: 调用javac ,需要将该jar添加到类路径中,如下所示:

javac -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample.java

I'm assuming that lwjgl-2.8.3 is actually a jar file. 我假设lwjgl-2.8.3实际上一个jar文件。 You never specified in your question. 您从未在问题中指定。

You'll have to once again add the jar to your classpath when you attempt to run your example: 尝试运行示例时,必须再次将jar添加到类路径中:

java -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample

The easiest way is to set the LWJGL folder to CLASSPATH environment. 最简单的方法是将LWJGL文件夹设置为CLASSPATH环境。 For example 例如

set CLASSPATH=${PATH_TO}/generic_folder/libs/lwjgl-2.8.3 设置CLASSPATH = $ {PATH_TO} /generic_folder/libs/lwjgl-2.8.3

That should do it. 那应该做。

When you run javac, you need to tell it where the jar file is. 运行javac时,需要告诉它jar文件在哪里。 You can either set you CLASSPATH environment variable or pass the location to the java compiler. 您可以设置CLASSPATH环境变量,也可以将该位置传递给Java编译器。 The latter is better, as you probably won't want lwjgl if you start a second java project. 后者更好,因为如果启动第二个Java项目,您可能不希望lwjgl。

To tell the java compiler directly, you would do something like 要直接告诉Java编译器,您可以执行以下操作

javac -classpath generic_folder/libs/lwjgl-2.8.3/lwjgl.jar DisplayExample.java

The classpath can either point to a jar file containing the classes or the top level directory containing unjared class files. 类路径可以指向包含类的jar文件,也可以指向包含未jar类文件的顶级目录。 If the unjarred classes are in generic_folder/libs/lwjgl-2.8.3/classes, you would point to generic_folder/libs/lwjgl-2.8.3/classes, not generic_folder/libs/lwjgl-2.8.3/org/lwjgl 如果未jarredred类位于generic_folder / libs / lwjgl-2.8.3 / classes中,则应指向generic_folder / libs / lwjgl-2.8.3 / classes,而不是generic_folder / libs / lwjgl-2.8.3 / org / lwjgl

javac -classpath external.jar myClass.java

有关更多信息-> https://stackoverflow.com/a/5112638/5790669

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

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