9

So I have packed my classes and their dependancies (apache commons cli) inside a jar file using one-jar (which was easy enough to do, see section Command-Line Approach). Now I am curious if I can run the java test class inside the jar using a Junit jar outside the class. So the path to the test class inside sw.jar is :

sw.jar\main\sw.jar\uoa\di\ys11\hw2\TestSmithWaterman.class

(the main\ is a one-jar thing). I have tried variations of :

java -jar -cp lib/junit.jar org.junit.runner.JUnitCore  uoa.di.ys11.hw2.TestSmithWaterman

with no luck - so what would the command line be ? Or do I need to modify the one-jar manifest somehow ?

EDIT : the /boot-manifest.mf:

Manifest-Version: 1.0
Main-Class: com.simontuffs.onejar.Boot
One-Jar-Main-Class: uoa.di.ys11.hw2.Hw2

while the /META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Created-By: 1.7.0_09 (Oracle Corporation)
Main-Class: com.simontuffs.onejar.Boot
One-Jar-Main-Class: uoa.di.ys11.hw2.Hw2
Roman C
  • 49,761
  • 33
  • 66
  • 176
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Good.. but can you edit it to *add a question?* – Andrew Thompson Jan 26 '13 at 13:16
  • @AndrewThompson:ok now added in the body (I thought the title would be enough, sorry) – Mr_and_Mrs_D Jan 26 '13 at 13:19
  • Stating "this is what I am trying to achieve" or "..want to do" is not actually a question. Asking a *specific* (which you have now done) makes the post fit better in a Q&A site and helps hone the answers. You've now asked some questions, good. Please make sure to add a question in future (without any prompt). – Andrew Thompson Jan 26 '13 at 13:23
  • Do you have any test methods in the class? Better to post the code. – Roman C Jan 26 '13 at 13:27
  • @RomanC: the code for the TestSmithWaterman class is here : https://www.dropbox.com/s/ykthr7v8rw7r0a8/TestSmithWaterman.java. The jar is here : https://www.dropbox.com/s/n0mbf6owbosr2y2/sw.jar – Mr_and_Mrs_D Jan 26 '13 at 13:30
  • 1
    You need to include the *entire* classpath if you use `-cp`. And you don't use `-jar` if you're not running a jar. – Dave Newton Jan 26 '13 at 13:44
  • @Dave : if you could make this work I'd appreciate it :D – Mr_and_Mrs_D Jan 26 '13 at 13:45
  • When using the `-jar` option, the `-cp` option is ignored. I don't know if this was the case in 2013, but it is now: https://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html – Adam Howell Jun 04 '21 at 14:31

1 Answers1

3

Use the following command line to test

java -cp lib/junit.jar;sw.jar org.junit.runner.JUnitCore  uoa.di.ys11.hw2.TestSmithWaterman

EDIT:

this should be work with normal jars, but the jar is created by the one-jar

One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.

After that it's not possible for junit, as I mention junit-4.4 in my case to load such classes for test.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • `java -cp lib/junit.jar;sw.jar org.junit.runner.JUnitCore uoa.di.ys11.hw2.TestSmithWaterman` results in `JUnit version 4.10 Could not find class: uoa.di.ys11.hw2.TestSmithWaterman Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing` etc – Mr_and_Mrs_D Jan 26 '13 at 14:04
  • sw.jar should be in the current folder or you should specify the full path to the jar – Roman C Jan 26 '13 at 14:08
  • it is in the current folder (by lib/) – Mr_and_Mrs_D Jan 26 '13 at 14:11
  • is lib in the current folder and sw.jar in the lib? If so, then use lib/sw.jar – Roman C Jan 26 '13 at 14:12
  • the class SelfDescribing is in the another jar should be added to the classpath (-cp) – Roman C Jan 26 '13 at 14:17
  • which brings us to the meat of the problem : if I do add it I get :`JUnit version 4.10 Could not find class: uoa.di.ys11.hw2.TestSmithWaterman Time: 0 OK (0 tests)` – Mr_and_Mrs_D Jan 26 '13 at 14:20
  • The problem lies in the structure of the jar - `java -cp lib/junit.jar;sw.jar;lib/org.hamcrest.core_1.1.0.v20090501071000.jar;lib/commons-cli-1.2.jar;bin/ org.junit.runner.JUnitCore uoa.di.ys11.hw2.TestSmithWaterman` works fine (bin/ is the usual Eclipse compile folder) – Mr_and_Mrs_D Jan 26 '13 at 14:39
  • If your TestSmithWaterman is another sw.jar that is in sw.jar as you written in the some weird path `sw.jar\main\sw.jar\uoa\di\ys11\hw2\TestSmithWaterman.class` then junit will not be able to load such class. Use simple jar created by jar command. – Roman C Jan 26 '13 at 14:53
  • so see ? you should read more carefully - accepted but still I believe it is possible - if someone comes up with a way - the dropbox links I posted in comments are valid (so if anyone wants to play with the jar...) – Mr_and_Mrs_D Jan 26 '13 at 15:24
  • Sure, it's great especially with the maven plugin, many people have troubles with creating executable jars with eclipse, even testers. – Roman C Jan 26 '13 at 15:29