运行具有多个 jar 文件和类的 java 程序 [英] Running a java program with multiple jar files and classes

查看:37
本文介绍了运行具有多个 jar 文件和类的 java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编译一个包含多个 jar 文件(在 lib 文件夹内)和类(在 src/com 文件夹内)的程序:

I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:

javac -classpath lib/* src/com/*.java

我输入这个来运行程序:

I typed this to run the program:

java -cp lib/* src/com/okc

但它不起作用.相反,我明白了:

But it doesn't work. Instead, I get this:

Error: Could not find or load main class src.com.okc

okc.java 是包含 main 方法的类.如何运行包含多个 jar 文件和类的 java 程序?

okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?

推荐答案

Java 类文件不仅仅是文件本身.代表类包的目录结构是类文件的一部分.你的类路径需要指向最顶层包目录的父目录.

A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.

假设您的类是用package com; 声明的,那么最顶层的包目录是com.所以你的类路径中需要 comparent:

Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:

java -classpath src:lib/* com.okc

如果你的类不包含任何package语句,而你刚好把它放在com目录下,那么它属于空包,其父目录是 com 本身:

If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:

java -classpath src/com:lib/* okc

附加说明:Java 约定让类名及其各自的文件名以大写字母开头.原因之一是它使类名易于与包组件区分开来.

An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.

这篇关于运行具有多个 jar 文件和类的 java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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