简体   繁体   English

如何从命令提示符制作jar文件

[英]How to make jar file from command prompt

I have been trying making jar file for my gradle project but always found error could not find or load main class CalculatorServer我一直在尝试为我的gradle项目制作 jar 文件,但总是发现错误could not find or load main class CalculatorServer

My compile (.class) files are in the following path in windows file system D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator我的compile (.class)文件位于 Windows 文件系统D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator中的以下路径中

When i trying to create jar file for Main class eg CalculatorServer using folloiwng command当我尝试使用以下命令为 Main 类创建 jar 文件时,例如CalculatorServer

D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator>java -cp test.jar com.test.calc.CalculatorServer

Following error occur could not find or load main class CalculatorServer出现以下错误could not find or load main class CalculatorServer

Edit编辑

Sorry I have posted wrong commands here.抱歉,我在这里发布了错误的命令。

My command for creating jar file is as follows我创建jar文件的命令如下

D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator> echo Main-Class: CalculatorServer >manifest.txt D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator>jar cvfm CalculatorServer.jar manifest.txt *.class D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator> echo Main-Class: CalculatorServer >manifest.txt D:\\Projects\\EclipseProjects\\CalculatorGRPC\\build\\classes\\main\\com\\grpc\\calculator>jar cvfm CalculatorServer.jar manifest.txt *.class

When i run this jar file当我运行这个 jar 文件时

`D:\Projects\EclipseProjects\CalculatorGRPC\build\classes\main\com\grpc\calculator>java -jar CalculatorServer.jar

Getting same error得到同样的错误

could not find or load main class CalculatorServer

You are in the wrong directory.你在错误的目录中。 No matter what are you trying to do, as long as that operation involves compiled class files, you should be in the output root, which is the XXXXX\\build\\classes\\main folder.无论您尝试做什么,只要该操作涉及已编译的类文件,您都应该在输出根目录中,即XXXXX\\build\\classes\\main文件夹中。

The reason for this is that java classloaders will try to find /com/foo/Bar.class in the jar if it is ordered to load class named com.foo.Bar .这样做的原因是 java 类加载器将尝试在 jar 中查找/com/foo/Bar.class如果它被命令加载名为com.foo.Bar类。 If you just pack a jar like that, the jar tool will place your classes right in the root directory of the output archive(you can confirm this by using a archive tool like 7z to investigate), which panic the classloader and gives you an error.如果您只是打包这样的 jar,jar 工具会将您的类放在输出存档的根目录中(您可以通过使用 7z 之类的存档工具进行调查来确认这一点),这会使类加载器恐慌并给您一个错误.

By the may, manifest should be named MENIFEST.MF adn placed under /META-INF directory, or same error would pop up again, if your manifest was written in a correct format.可能,manifest 应该命名为 MENIFEST.MF 并放置在 /META-INF 目录下,否则如果您的 manifest 以正确的格式编写,则会再次弹出相同的错误。

PS: Why the heck you are doing these all by your bare fist? PS:你为什么要光着拳头做这些? This is what gradle is supposed to do, or why is it called build automation?这就是 gradle 应该做的,或者为什么它被称为构建自动化? There should be your desired jar files somewhere under the /build/ directory, typically /build/lib/ . /build/目录下应该有你想要的 jar 文件,通常是/build/lib/

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

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