简体   繁体   English

如何从命令行运行Eclipse编译的Java类?

[英]How can I run an Eclipse compiled Java class from the command line?

I am writing a basic TCP chat program and one of the requirements is that it can be run from the command line with the following argument formats: 我正在编写一个基本的TCP聊天程序,其中一项要求是可以使用以下参数格式从命令行运行该程序:

java Server 8888
java Client localhost 8888

Which will start a server listening on 8888 and which waits to accept incoming connections from clients. 它将启动侦听8888的服务器,并等待接受来自客户端的传入连接。 Then start a client and connect to the server at localhost:8888. 然后启动一个客户端,并在localhost:8888连接到服务器。 These classes can both be compiled and run from within Eclipse and I have added the above variables to the run configurations for the classes, respectively. 这些类都可以在Eclipse中进行编译和运行,我分别将上述变量添加到了这些类的运行配置中。

If I navigate to the directory of the files in CMD I can see the compiled .class files, but when I try to run the server with: 如果导航到CMD中的文件目录,则可以看到已编译的.class文件,但是当我尝试使用以下命令运行服务器时:

java Server 8888

I get the error 我得到错误

Error: Could not find or load main class Server

Eclipse>Window>Preferences>Java>Compiler shows JDK 1.7 . Eclipse> Window> Preferences> Java> Compiler显示JDK 1.7

Running java -version from the command line shows 从命令行运行java -version显示

java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)

I want to be able to run both the classes from separate prompts in parallel. 我希望能够从单独的提示中并行运行两个类。 Any ideas? 有任何想法吗?

java -cp . basicChat.Server 8888

You need to specify the fully qualified class name (including the package name) 您需要指定完全限定的类名称(包括包名称)

Reason: 原因:

The class full name (known as fully qualified name) is not Server , it is basicChat.Server . 类全名(称为全限定名)不是Server ,而是basicChat.Server The file is located under a directory named basicChat . 该文件位于名为basicChat的目录下。 So java is looking for aa directory structure matching the package name. 因此,java正在寻找与包名称匹配的目录结构。 The Server.class file is located under the basicChat directory in the file system. Server.class文件位于文件系统中basicChat目录下。

Else consider how we would have a problem to select the intended class if you had several classes called Server in different packages (name spaces). 否则,如果您在不同的程序包(名称空间)中有多个称为Server类,那么考虑如何选择所需的类。

Are you certain that in both these classes you have main method? 您确定在这两个类中都有main方法吗?

public static void main(String[] args) { ... }

And do your classes have public modifier? 而且您的课程有public修饰符吗?

if you have this class in a package eg: test.Server you'll run it from bin folder like that: 如果您在例如test.Server的程序包中包含此类,则可以从bin文件夹运行test.Server ,如下所示:

java test.Server 8888

Compiling this class: 编译此类:

public class Server {
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(args));
    }
}

and running: 并运行:

java Server 8888

gives output: 给出输出:

[8888]

Your CLASSPATH is incorrect. 您的CLASSPATH错误。 Try the following (on Windows) from bin directory: 在bin目录中尝试以下操作(在Windows上):

java -cp .;%CLASSPATH% basicChat.Server 8888

Also, you can run your Server in Eclipse. 另外,您可以在Eclipse中运行服务器。 Create a run configuration for it, specify a port in arguments. 为其创建运行配置,并在参数中指定端口。 Then you can launch it in Eclipse, it will be running util you stop it or close Eclipse. 然后,您可以在Eclipse中启动它,它将在您停止或关闭Eclipse的情况下运行。

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

相关问题 如何从Eclipse编译的命令行运行Java类文件?外部Jar问题 - How to Run a Java Class File from the Command Line that was Compiled by Eclipse? External Jar problems Java:如何在命令行上运行此经过Eclipse编译的程序? - Java: How do I run this eclipse-compiled program on the command line? 无法在命令行中运行由eclipse编译的类文件 - Cannot run class files compiled by eclipse in command line 如何从Java在命令行中运行ruby文件? - How can I run a ruby file in command line from Java? 如何调试使用 Eclipse Z581D6381F01F35E4B36ACDZ7 文件编译的命令行 Java.class 文件? - How to debug a command line compiled Java .class file using Eclipse IDE? 我可以从Eclipse创建的命令行程序运行吗? - Can I run from command line program created by Eclipse? 我可以从命令行运行Eclipse项目,但无法通过Eclipse运行它 - I can run an Eclipse project from command line but I cannot run it via Eclipse 当我从命令行运行 Main 时,Java 在同一个 package 中找不到 class - Java can't find class in same package when I run Main from command line 我如何在命令行中为intellij项目运行java类 - How could I run java class in command line for a intellij project 如何从命令行运行增量编译的NetBeans应用程序? - How do I run an incrementally-compiled NetBeans application from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM