简体   繁体   中英

How do I create a jar using the command line where a class is dependent on another class in the same project?(no build tools please!)

I'm trying to learn java, more so as a hobby than anything else, taking a different angle from my usual C# where you actually have namespaces and multiple classes in a file(not ranting or anything). However I don't seem to quite understand how the jar creation process seems to work, at least regarding the "files-input" parameter.

I am well aware that there are build tools like and,maven,etc. I just want to have a better understanding of the entire process that's all.

I'm trying to create a jar out of my 2 classes which are in 2 separate packages.

Here is the folder structure:

tryproject\\packageOne\\MainApp.class
tryproject\\packageTwo\\Greeter.class

MainApp.java

package packageOne;

import packageTwo.Greeter;

public class MainApp{

 public static void main(String[] args){    
     Greeter greeter=new Greeter();
     greeter.setMessage("Hello World");
     greeter.sayHello();
 }
}

Greeter.java

package packageTwo;

public class Greeter{

 private String whatTosay;

 public void setMessage(String whatTosay){
    this.whatTosay=whatTosay;
 }

 public void sayHello(){
    System.out.println(whatTosay);
 }
}

Here is the command prompt output:

C:\Users\SomeUser>jar cfve tryapp.jar MainApp C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class
added manifest
adding:Users/SomeUser/Desktop/LearningJava/tryproject/packageOne/MainApp.class(in = 414) (out= 299)(deflated 27%)
adding:Users/SomeUser/Desktop/LearningJava/tryproject/packageTwo/Greeter.class(in = 506) (out= 333)(deflated 34%)

C:\Users\SomeUser>java -jar tryapp.jar
Error: Could not find or load main class MainApp
Caused by: java.lang.ClassNotFoundException: MainApp

I don't really understand why it doesn't find my MainApp class and I'm also curious if it is possible to build the jar this way without a pre-made manifest.

For anyone who stumbles upon this post, the reason of confusion, and the reason this didn't work out for me, is because I didn't take into account the fact that I was creating my jar somewhere else than in the root of a drive so the jar structure would look something like this:

Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class
Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class

The above is how the jar structure looks like after I created it with the following command:

C:\Users\SomeUser>jar cfve tryapp.jar MainApp C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class

Now in order to run this jar you have to change the above command so that instead of having just MainApp there you have to put the entire project structure like this:

C:\Users\SomeUser>jar cfve tryapp.jar Users.SomeUser.Desktop.LearningJava.tryproject.packageOne.MainApp  C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class

And also you have to modify the package of each .java class file to resemble the structure I just modified in the command. So instead of having just package packageOne; or package packageTwo; you need to rewrite them like this package Users.SomeUser.Desktop.LearningJava.tryproject.packageOne; and package Users.SomeUser.Desktop.LearningJava.tryproject.packageTwo; . The same goes for import. Now if you modified all your .java files and recompiled them you should be able to run the jar using the command instruction that I mentioned earlier.

But a better solutions, and an easier one is like @Prashant Gupta mentioned above.

For instance just use:

jar cfve tryapp.jar packageOne.MainApp -C C:\Users\SomeUser\Desktop\LearningJava\tryproject\ .

But it will get all the files in that directory so maybe just have a separate directory for binary files or all file you need for your application and another for the source files.

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