简体   繁体   中英

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

My compile (.class) files are in the following path in windows file system 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

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

Edit

Sorry I have posted wrong commands here.

My command for creating jar file is as follows

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

`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.

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 . 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.

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.

PS: Why the heck you are doing these all by your bare fist? This is what gradle is supposed to do, or why is it called build automation? There should be your desired jar files somewhere under the /build/ directory, typically /build/lib/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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