JUnit Could not find class: package_name.class - java

I just dropped the JUnit folder into /System/Library/Java/Extensions/. I'm able to get JUnit to run, but it can't find my test class.
I'm running this command (plus a few variants) from the containing folder of the package (/containing_folder/package_name/):
java org.junit.runner.JUnitCore package_name.ClassTest
but it tells me:
JUnit version 4.10
Could not find class: package_name.ClassTest
My system info:
MacOS 10.7.2 • Java 1.6.0_26 • JUnit 4.10
Addendum:
I've moved JUnit per suggestion and tried running JUnit on my test class with the following bash script, but I'm still getting the same error message.
#!/bin/bash
export CLASSPATH=/Users/myname/Desktop/Programming/Java/junit4.10/junit-4.10.jar:/Users/myname/Desktop/Programming/Java/:/Users/myname/Desktop/Programming/Java/package_name.jar
java org.junit.runner.JUnitCore package_name.ClassTest
Any *nix or mac users see what I'm doing wrong?
Thanks.
P.S. No, "myname", "ClassTest", and "package_name" are not the real names used on my system - they've been anonymized.

You can use -cp option.
java -cp class_path_or_jar_separated_by_comma class_to_run

Goood Morning,
the trick is (like Dave Newton already said) to set the Classpath right. In a batch file it would look like this:
set CLASSPATH=D:\_zip\lib\junit.jar;D:\containing_folder
java org.junit.runner.JUnitCore package_name.ClassTest
The important thing here is to have the following things in your Classpath:
JUnit class files
Your class files, including your JUnit test classes
Libraries your class files depend on
This means you have to set the containing folder of your class files (in my example it's D:\_zip) in the classpath. So you can set the full classname (including packages) as an argument for the JUnitCore.
A good FAQ is here: http://junit.sourceforge.net/doc/faq/faq.htm#running_1
Update: I'm not sure how the bash script und MacOS works, but if you have your classes packed in a JAR file, it would be enough, to set the CLASSPATH inside the JAR files' manifest:
Manifest-Version: 1.0
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Built-By: Gruber ^^
Implementation-Vendor: Company
Implementation-Title: Title
Implementation-Version: 1.0
Main-Class: package_name.ClassTest
Class-Path: /Users/myname/Desktop/Programming/Java/junit4.10/junit-4.10.jar
The Main-Class is not really important for the JUnit tests, but i left it here. The manifest file is set inside the JAR:
package_name.jar/META-INF/MANIFEST.MF
You can set it manually or Eclipse will set it for you (export to jar, etc), if you use it ^^.
With this Manifest set, the script should look like this (called from the same directory as the jar file):
set CLASSPATH=package_name.jar
java org.junit.runner.JUnitCore package_name.ClassTest

Related

How to use a jar file with another jar file [duplicate]

I have jar file with manifest in it
Manifest-Version: 1.0
Build-Jdk: 1.7.0_67
Created-By: Apache Maven 3.2.3
Main-Class: com.company.main.Main
Archiver-Version: Plexus Archiver
And the jar has compile dependency to external library
compile 'org.apache.commons:commons-lang3:3.3.2'
So I want to execute it comandLine I wrote:
java -cp C:\commons-lang3-3.3.2.jar -jar myJar-1.0.0.jar
But
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at ...
How to add this file on the class path?
PS. If I use the "bad way" and copy-paste commons-lang3-3.3.2.jar in the ...jre7\lib\ext folder. Everything is working.
If you're not using the -jar option then you need to specify the main class to run, as the manifest will not be interrogated:
java -cp C:\commons-lang3-3.3.2.jar;.\myJar-1.0.0.jar com.company.main.Main
The classpath (-cp) option is ignored if using the -jar option (in that case the manifest should reference any other required jars via its classpath directive).
"Bad Way"..? You should never include your jar files inside the java directories.. How do you expect the users of your application to use your jar when they are using the standard java..?
either you can use the command suggested by #tombola82 or you can include the commons-lang jar in your project itself so that you can refer it.

executable jar Could not find or load main class

I've been struggling with this common error and just can't resolve it. This application is composed of multiple packages and runs fine within JCreator (at the moment I need to use this IDE rather than Eclipse).
My manifest file is here (there are 2 blank lines at the end):
Manifest-Version: 1.0
Created-By: 1.6.0_45 (Sun Microsystems Inc.)
Main-Class: C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
I wrote a bat file to create the jar:
jar -cvfm DPlus.jar C:\COMPILE\MyProjects\douwe\classes\MANIFEST.MF
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\library*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\types*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\util*.class
When I try to execute with the command
C:\COMPILE\MyProjects\douwe\classes>java -jar DPlus.jar
I always get the error:
Error: Could not find or load main class C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
Can anyone see what I'm doing wrong here?
Usually this error is due to MANIFEST.MF if the'res no application's entry point has been set.
Your manifest file should have this line of code
Main-Class: YourPackage.DPMain
Alternatively, you can do the following.
java -cp .;app.jar YourPackage.DPMain
In my implementation, there are something different from yours, you can refer:
(1) The folder(before compressed) structure
you need to add a META-INF folder and put your MANIFEST.MF in it
(2) The content in your MANIFEST.MF
I think your should use the package format instead of a folder tree format:
Manifest-Version: 1.0
Main-Class: com.loadtest.mgr.LoadTestStarter

Failed to load Main-class Manifest Attribute

I am getting following error while executing compiled jar file. I have re installed my java but my problem is not solved yet.
Failed to load Main-class Manifest Attribute from
D:\Tools\Lodable_Creation\dist\Lodable_Creation.jar
Currently by MANIFEST.MF file looks like.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.1
Created-By: 1.6.0-b105 (Sun Microsystems Inc.)
Main-Class: main
X-COMMENT: Main-Class will be added automatically by build
I am using Netbeans 6.9.1 IDE.
Use a package for your class. Make sure your class looks something like this (notice the package and the public class):
package com.foo;
public class Main {
public static void main(String[] args) {
}
}
After which you can specify Main-Class as so:
Main-Class: com.foo.Main
As adarshr suggested, JVM is not able to find the class because it requires the fully-qualified name in the Main-Class attribute of Manifest file.
Actually, it is not really necessary to specify the main file. You can just give your JAR file as your classpath, and give the fully-qualified name of the class to run it using java.
Say your JAR is myJar.jar and the fully-qualified main file is com.user.Main. Then from the command line, go to the directory which has your JAR File and give :-
java -classpath myJar.jar com.user.Main
And this will run the Main class. You would also need to give the classes (or JARs) in the classpath which are used (imported) in your Main class.
See this link for the details.
I have encountered this error when I have developed projects with a JDK(1.7 in my case) and the installed JRE was an older version(1.6). Try to update your JRE or change the JDK used, if possible, to match your JRE version.

[TestNG][Jar] Set Classpath in Manifest (Class-Path) -- classpath is ignored?

I'm trying to run a simple TestNG test case residing within a jar file, which
contains the test and manifest:
ex.) Test.jar contains:
{
META-INF\
META-INF\MANIFEST.MF
tests\
tests\Test01.class
}
I am trying to run it using the command: java org.testng.TestNG -
testjar Test.jar testng.xml
Where a folder contains (all in the same directory):
Test.jar
testng.xml
testng-6.1.1.jar
And Manifest contains (w/ a line-break at the end):
Manifest-Version: 1.0
Created-By: Willie Owens
Class-Path: testng-6.1.1.jar Test.jar .
And I get a NoClassDefFoundError: org/testng/TestNG. Could not find
main class.
If I specify the classpath using -cp after "java" (Ex. java -cp
testng-6.1.1.jar;Test.jar org.testng.TestNG -testjar Test.jar
testng.xml) it works, but I want this information in the manifest.
What am I doing wrong here?
Also, I've tried every variation I could think of when typing in the Class-Path, such as: ./testng-6.1.1.jar .\testng-6.1.1.jar ...etc..
HELP
Executing test-jar is not that complex with testNg please refer [1] for more detail.
lease let me know if you find any pitfalls with it .
[1] http://dharshanaw.blogspot.com/2012/10/how-to-execute-testng-tests-in-side.html

Jar Can't Find Main Class

I get: Could not find the main class: org.dav.kin.Tester. Program will exit. when I attempt to run my jar file via java -jar tester.jar or java -classpath tester.jar org.dav.kin.Tester Does anyone know what is wrong and how to fix it? Below are additional details. Thanks.
Manifest File:
Manifest-Version: 1.0
Created-By: DKin
Class-Path: .
Main-Class: org.dav.kin.Tester
jar tf tester.jar
org/
org/dav/
org/dav/kin/
org/dav/kin/Tester.class
org/dav/kin/TesterCellRenderer.class
...
...
META-INF/
META-INF/MANIFEST.MF
UPDATE:
Jar file runs if I specify the system classpath, which contains the groovy-all-{version}.jar, like so: java -classpath tester.jar;"%CLASSPATH%" org.dav.kin.Tester Anyone know why I have to explicitly re-state the classpath (or more precisely, the groovy jar)?
Your jar file lacks a file with this name
/org/dav/kin/Tester.class
or you have special characters in your MANIFEST.MF file
MANIFEST.MF files have a particular syntax. It's best to use other tools to generate them; however some of the details I've encountered which increases the success of hand written files include:
Always make sure the lines are less than 72 characters long.
Always use \r\n (windows newline), even on non-windows systems.
Verify that all whitespace characters are spaces.
Verify that there are no nonprintable characters (htab, etc).
Sometimes a blank line at the end of the file helps.
Is Tester.class' package declaration org.dav.kin?
You have indicated that the you are using Groovy. Groovy does compile down to Java class files but it still requires the groovy runtime libraries. You need to make sure groovy is on the classpath as well as your classes. Try this:
java -classpath tester.jar;groovy-all-1.8.0.jar org.dav.kin.Tester
Just in case. I just solve exactly the same problem.
Instead of
Class-Path: .
in MANIFEST.MF
one should enumerate (with space) jars which are required in runtime, so it should be something like this:
Class-Path: groovy-all-2.4.5.jar relative/my-dependent-project-artifact.jar

Categories

Resources