0

is there any diff between run a java program with jar or with a package unpacked? now i meet a Weird probem. i have a search program.when i run it with a jar,it's ok. when i run it just with package unpacked to jar ,then the gc log is a

  • a example: 0.00: [GC 275953K->145708K(1325120K), 0.0260210 secs] > 0.833: [GC 277260K->144889K(1322688K), 0.0283940 secs] > 1.833: [FULLGC 577260K->344889K(1322688K), 1.0283940 secs] > 1.833: [FULLGC 777260K->844889K(1322688K),2.0283940 secs] > 1.833: [FULLGC .... – allwefantasy Jan 13 '11 at 07:36

2 Answers2

1

There is no difference from JVM perspective. JVM knows to load classes from file system or from zip file transparently.

I do not exactly understand which GC log is this but I strongly believe that if you have any difference in running your java program from jar or from unpacked classpath it may be caused by

  • differences in real classpath
  • problems to access specific path in file system (that cause differences in real classpath)
  • differences in options you are passing to JVM (-D and -X options)
  • probably other differences in environment. for example probably you did your first run and user A and second as user B. Or probably you changed working directory.

And another option is if the program you are trying to run deals programmatically (on application layer) with its own classpath. I saw program that assumes that it must be executed from jar named like mycompany.jar. Otherwise it did not work.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • for example||||| assume current direcory have SearchServer.jar and a com dir which holds class files.they both are building from the same git repo.||||||| |||||java -Xms1300m -Xmx1300m -XX:PermSize=128m -Xloggc:gc.$port.log -XX:+PrintGCTimeStamps -XX:-PrintGCDetails -cp $classpath com.SearchServer p 8088 |||||| java -jar -Xms1300m -Xmx1300m -XX:PermSize=128m -Xloggc:gc.$port.log -XX:+PrintGCTimeStamps -XX:-PrintGCDetails -cp $classpath SearchServer.jar p 8088 – allwefantasy Jan 13 '11 at 08:03
  • I am investigating a problem with `log4j2` and I can confirm it's not the same. With a jar my program runs OK, but if I unzip that jar, there's some log4j logic that breaks. In particular, I am creating a dynamic appender (with jar it works, with folder it doesn't receive the log events). – Ferran Maylinch Apr 12 '23 at 12:38
  • @FerranMaylinch this sounds like a bug in the log4j2 appender that you are using. Theoretically everything is possible. One can investigate its own classpath and manipulate with it. But if somebody does this he/she should be aware on the fact that the application can load classes from filesystem, from jar, from jar packed in another jar (like spring boot), from URL etc. – AlexR May 31 '23 at 06:13
1

To expand slightly on the answer from AlexR; There is no difference from the JVM point of view, however there can be difference from a resource point of view.

That is:

java -cp test.jar com.company.test

and

unzip -d test test.jar
java -cp test com.company.test

Can produce different results in certain very specific circumstances. The only one that I can think of off the top of my head is to when reading a ZIP file resource. The standard resource reader that retrieves a resource from a JAR file, cannot be then passed to ZipFileReader, whereas a resource passed in from a file on disk can be.

That said, given the differences that you are seeing, this is unlikely to be your problem, and the course is almost certainly one of:

  • different command line arguments, either directly or via an environment variable
  • different classpaths. Note that java -jar test.jar and java -cp test.jar com.company.test are not the same thing, and could have different classpaths.

Look at AlexR's answer for other suggestions.

Community
  • 1
  • 1
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74