Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Java classpath solution

Tags:

java

classpath

I am trying to figure out a way of going around the problem of a long classpath in windows. Although I am in Linux (that is why the examples bellow are on a unix format), the end users may be in linux and I know the problem happens.

My application download and create a string of the jars and files it needs to run. It than generates a string such as:

java -classpath path_to_file1:path_to_file2:path_to_file3 jvm_args MainClass

And runs the application. This works fine in Linux but it explodes in windows because the classpath is too long.

The problem I understood and I have tried the solution proposed on: How to set a long Java classpath in Windows?

However I had no success since the path to the MainClass is not found (it is on the classpath!). Here is what I did:

I have the following folder structure:

[root]
├── classes
│   └── com
│       └── tst
│           ├── CPTest1.class
│           ├── CPTest2.class
│           └── CPTest3.class
└── com
    └── tst
        ├── CPTest1.java
        ├── CPTest2.java
        └── CPTest3.java

the classes were compiled by calling:

javac -d classes -cp . com/tst/*

And here are the files:

CPTest1.java

package com.tst;

public class CPTest1{
    public static void main(String[] args) {
        System.out.println ("Run CPTest1.main()");
    }
}

CPTest2.java

package com.tst;

public class CPTest2 {
    public static void main(String[] args) {
        System.out.println ("Run CPTest2.main()");
        CPTest1 cpt1 = new CPTest1();
    }
}

CPTest3.java

package com.tst;

public class CPTest3 {
    public static void main(String[] args) {
        System.out.println ("Run CPTest3.main()");
        CPTest1 cpt1 = new CPTest1();
        CPTest2 cpt2 = new CPTest2();
    }
}

Now if I am on [root] and run:

java -cp classes com.tst.CPTest3

I see the correct output. However if I first generate the "pathing jar" as the solution I pointed out suggests, by creating a file: cpTest.txt (I've tried this with relative and full path)

Class-Path: classes

and create the jar using:

jar -cvfm cp.jar cpTest.txt

than finally trying to run:

java -cp cp.jar com.tst.CPTest3

all I get is an error of not finding the CPTest3 class:

Error: Could not find or load main class com.tst.CPTest3

What am I missing? Thanks for any help!

like image 342
fditz Avatar asked Apr 01 '26 06:04

fditz


1 Answers

Use java -jar and put all the dependent JAR files into the main JAR file's Class-path manifest entry.

like image 167
user207421 Avatar answered Apr 02 '26 19:04

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!