3

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!

fditz
  • 871
  • 9
  • 28
  • Are you sure that com.tst.CPTest3 is in the cp.jar? You can easily check that by unzipping the jar. Look also at maybe using a manifest file if you need many jars on the classpath: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html – Nicola Ambrosetti Aug 24 '17 at 11:04
  • does this only work with jars? The jar I created does not contain anything other than the Manifest file with the class-path entry. I understood that was the idea behind the answer on the post I mentioned – fditz Aug 24 '17 at 12:29
  • Possible duplicate of [How to set a long Java classpath in Windows?](https://stackoverflow.com/questions/201816/how-to-set-a-long-java-classpath-in-windows) – rghome Dec 12 '17 at 10:13
  • As you've might have seen I have mentioned this issue in the description. So it is not a duplicate, but an attempt of solution which I didn't understand the behaviour – fditz Dec 18 '17 at 10:19

1 Answers1

1

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • well that is what I did by creating the "pathing jar" wasn't it? I mean in my example I am not using other jars, but just classes... I wanted to try this since in my actual project I have JAR's as well as Manifests, images and so on and so forth – fditz Aug 24 '17 at 12:28