如何在命令提示符下运行 Java 程序 [英] How to run Java program in command prompt

查看:37
本文介绍了如何在命令提示符下运行 Java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Java 项目来调用 Web 服务.它有一个 Main java 文件和另一个类文件.我为 HTTP 客户端使用了一些 jar 文件.
在 Eclipse 中它运行良好.我需要通过传递一些参数在命令提示符下运行 Java 程序.
在命令提示符中,我转到包含主 java 和子类 java 文件的 src 文件夹,并给出以下命令

javac mainjava.java

我收到以下错误

<块引用>

ma​​injava.java:14:找不到符号
符号:类子类

这里的 SubClass 是我用来调用 web 服务的另一个 java 类文件.

如何通过传递参数来运行程序?

解决方案

javac 是 Java 编译器.java 是 JVM 和用于执行 Java 程序的东西.您不执行 .java 文件,它们只是源文件.大概有 .jar 某处(或包含 .class 文件的目录)是在 Eclipse 中构建它的产物:

<前>java/src/com/mypackage/Main.javajava/classes/com/mypackage/Main.classjava/lib/mypackage.jar

从目录java执行:

<块引用>

java -cp lib/mypackage.jar 主 arg1 arg2

I created a Java project to call a Web service. It has one Main java file and another class file. I have used some jar files for HTTP client.
In Eclipse it runs fine. I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command

javac mainjava.java    

I'm getting following error

mainjava.java:14: cannot find symbol
symbol : class SubClass

here SubClass is my another java class file used to call the web service.

How to run the program by passing arguments?

解决方案

javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files. Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:

java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar

From directory java execute:

java -cp lib/mypackage.jar Main arg1 arg2

这篇关于如何在命令提示符下运行 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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