0

I have maven project with structure like this:

┌─package1
│ class1
│   ├─public static void main
│ class2
│   ├─public static void main
│ class3
│   └──public static void main
│
├─package2
│   └──some classes
├─package3
│   └──some classes
└─packageN
    └──some classes

To be able to run class1 or class2 or class3 I've exported each class to runnable jar file.

How can I achieve a single jar file where I choose class (from package1) using argument?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
pburgr
  • 1,722
  • 1
  • 11
  • 26

1 Answers1

2

You're probably already done. Jar the files and run java -cp <jarfile> package1.class1 or java -cp <jarfile> package1.class2 or java -cp <jarfile> package1.class3

The -cp specifies the classpath; in this case you want your jar file. And then you name the class you want to use as an entrypoint.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • this gives me `Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/jdesktop/swingx/JXHyperlink` I'm using `org.jdesktop.swingx.JXHyperlink;` in package2. – pburgr Oct 08 '21 at 12:25
  • @pburgr Add your jdesktop jar to the classpath. Add all your dependencies to the classpath. You can probably [use maven to build a fat jar](https://stackoverflow.com/q/16222748/2970947). – Elliott Frisch Oct 08 '21 at 12:29
  • The jar files are added OK, I've made new export with `Extract required libraries...` instead of `Package required libraries...` and it's working as I need. Many thanks. – pburgr Oct 08 '21 at 12:48