简体   繁体   English

如何运行Java文件?

[英]How to run a Java file?

There is a java program in the folder 文件夹中有一个Java程序

Downloads/jason/src/demo/de/l3s/jason/testfile.java

I can compile it with javac testfile.java (while in that directory) Resulting in testfile.class . 我可以使用javac testfile.java (在该目录中)进行编译,从而生成testfile.class

But I cannot run this file by typing java testfile If I manage to run the file, it cannot locate any components of the file. 但是我无法通过键入java testfile来运行此文件。如果我设法运行该文件,它将无法找到该文件的任何组件。

The testfile.java contains testfile.java包含

 package de.l3s.jason.demo;

 import java.net.URL;

 import de.l3s.jason.extractors.ArticleExtractor;

 public class testfile 

And then a couple of lines of code. 然后是几行代码。

How do I tell Java where to look for these components? 如何告诉Java在哪里寻找这些组件?

I have added several jar files to the classpath (per the documentation) by copying them directly into home/usr/lib/jvm/java/jre/lib/ext 通过将它们直接复制到home/usr/lib/jvm/java/jre/lib/ext我将一些jar文件(根据文档)添加到类路径中

Aside from that, the classpath works fine with "java" and "javac" working from anywhere. 除此之外,类路径还可以在任何地方使用“ java”和“ javac”正常工作。

I couldn't get this to work on Windows either, which is why I switched to linux. 我也无法在Windows上使用它,这就是为什么我切换到Linux的原因。

try 尝试

java de.l3s.jason.demo.testfile

But first make sure it contains a 但首先请确保它包含一个

public static void main (String [] args);

尝试:

java de.l3s.jason.demo.testfile

Please try java de.l3s.jason.demo.testfile . 请尝试java de.l3s.jason.demo.testfile Maybe you need to add more jar-Files to the classpath then that, so please enter the result here. 也许您需要在类路径中添加更多的jar文件,因此请在此处输入结果。

Assuming you have compiled all classes within the source tred and the compiled classfiles are stored in the directory where your .java file is, then you need to use the following command. 假设您已经编译了源代码中的所有类,并且已编译的类文件存储在您的.java文件所在的目录中,那么您需要使用以下命令。

java -cp Downloads/jason/src/demo de.l3s.jason.testfile

Note the usage of the -cp switch to specify the classpath's root directory (where the package structure starts) 注意-cp开关用于指定类路径的根目录(包结构开始的地方)

If the classes need additional libraries, you need to include them in the -cp switch: 如果这些类需要其他库,则需要在-cp开关中包括它们:

java -cp Downloads/jason/src/demo:/some/dir/somelib.jar:/other/dir/otherlib.jar  de.l3s.jason.testfile

(Assuming you are on a Unix style operating system. On Windows you need to use ; as the path separator and of course a backslash instead of a forward slash) (假设您使用的是Unix风格的操作系统。在Windows上,您需要使用;作为路径分隔符,当然还要使用反斜杠而不是正斜杠)

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

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