如何从终端运行Java程序? [英] How to run Java programs from the terminal?

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

问题描述

我正在尝试从我的终端运行一个Java程序。我有Mac OS X 10.7。


teamL javac -jar kxml2-2.3.0.jar XMLHandler.java ServiceEndpoint.java TeamL.java


这是我的Eclipse类文件结构:





我无法找到为什么这个投掷无法访问jarfile kxml2-2.3.0.jar

解决方案

如果您引用任何外部库,则必须将它们添加到 classpath 中。您可以在编译课程时添加它。



转到src目录:

  javac -classpath。:< path_to_jar_file> teamL / *。java 

执行:

  java -cp。:< path_to_jar_file> teamL< CLASS_NAME> 

如果您使用eclipse,则转到< project_directory> / bin / 这里你可以找到编译的类(所以你不必编译它们),并使用上面的 java 命令直接运行它们



注意:由于您的类是在 teamL 包下打包的,因此必须通过指定完全限定的方式从包外部运行类名称像 teamL.ServiceEndpoint


I am trying to run a Java program from my Terminal. I have Mac OS X 10.7.

teamL javac -jar kxml2-2.3.0.jar XMLHandler.java ServiceEndpoint.java TeamL.java

This is my Eclipse class file structure:

I am not able to find why is this throwing Unable to access jarfile kxml2-2.3.0.jar?

解决方案

If you are referencing any external libraries, then you have to add them to the classpath. You can add it during compilation of the classes this way.

Go to the src directory and :

javac -classpath ".:<path_to_jar_file>" teamL/*.java

TO execute :

java -cp ".:<path_to_jar_file>" teamL.<class_name>

if your are using eclipse, then go to <project_directory>/bin/ here you can find the compiled classes (so you dont have to compile them) and directly run them using the above java command

Note: Since your classes are packaged under teamL package, you have to run the classes from outside the package by specifying the fully qualified name like teamL.ServiceEndpoint

这篇关于如何从终端运行Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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