I want to run TestNG without using the "TestNG eclipse plugin" in a normal maven quickstart project.
Whatever I read from http://testng.org/doc/documentation-main.html#running-testng-programmatically & other sources, I got idea of using classes as:
MessageUtil.java
package com.mytests.testng;
public class MessageUtil {
private String message;
public MessageUtil(String message){
this.message = message;
}
public String printMessage(){
System.out.println(message);
return message;
}
}
TestNGExample.java
package com.mytests.testng;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNGExample {
String message = "Hello World";
MessageUtil messageUtil = new MessageUtil(message);
#Test
public void testPrintMessage() {
Assert.assertEquals(message,messageUtil.printMessage());
}
}
App.java
package com.mytests.testng;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testNG = new TestNG();
#SuppressWarnings("rawtypes")
Class[] testClasses = new Class[]{
TestNGExample.class
};
testNG.setTestClasses(testClasses);
testNG.addListener(tla);
testNG.run();
}
}
But here I am getting exceptions as:
Exception Box 1
Exception Box 2
Exception in Console
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.testng.ITestListener
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
I am not getting why there is a problem even though I don't get any compile time error. The TestNG dependency is successfully downloaded and helps in writing the code. But the class doesn't load on run time I guess. What can be done?
If not using any XML is causing an error, I can use it as well, but not the eclipse plugin for TestNG.
P.S.: I am new to TestNG and this is my first program
This issue is happening because at runtime, testng jar is not available in classpath. You can resolve this by using <scope>compile</scope> instead of <scope>test</scope> in your testng pom dependency.
Related
trying to run simple test selenium Web Driver program which is mentioned below:
package com.abc;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Mock_Exam {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
}
}
after running as java application getting the below error :
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/JsonSyntaxException
at org.openqa.selenium.firefox.Preferences.readDefaultPreferences(Preferences.java:95)
at org.openqa.selenium.firefox.Preferences.<init>(Preferences.java:65)
at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:87)
at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:77)
at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:66)
at org.openqa.selenium.firefox.FirefoxDriver.getProfile(FirefoxDriver.java:262)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
at com.kesdee.ia.Mock_Exam.main(Mock_Exam.java:14)
Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonSyntaxException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
FireFox version: 36.0.1
Selenium: selenium-java-2.45.0
You miss the gson-<version>.jar: in your classpath. Download add add it.
I am writing an Eclipse plugin that supports automated testing like JUnit. I have a Launch Configuration Delegate that launches my Main class, which will run the tests on the class the user provides.
I want to programmatically show a custom View from my Eclipse plugin when I launch it with my Launch Configuration Delegate. I keep getting a NoClassDefFoundError for the AbstractUIPlugin class when I try to launch it even though I have included both org.eclipse.ui and org.eclipse.core.runtime in my plugin dependencies.
Stack Trace
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/ui/plugin/AbstractUIPlugin
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at eoo.thefamilycoder.specj.internal.launching.delegate.SpecJMain.main(SpecJMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.eclipse.ui.plugin.AbstractUIPlugin
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SpecJ
Bundle-SymbolicName: eoo.thefamilycoder.specj;singleton:=true
Bundle-Version: 1.0.0.alpha
Bundle-Activator: eoo.thefamilycoder.specj.internal.Activator
Require-Bundle: org.eclipse.ui;bundle-version="3.105.0",
org.eclipse.core.runtime;bundle-version="3.9.0",
org.eclipse.jdt.launching;bundle-version="3.7.0",
org.eclipse.debug.core;bundle-version="3.8.0",
org.eclipse.jdt.core;bundle-version="3.9.1",
org.eclipse.debug.ui;bundle-version="3.9.0",
org.eclipse.jdt.ui;bundle-version="3.9.1",
org.eclipse.jdt.debug.ui;bundle-version="3.6.200"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Main class
import eoo.thefamilycoder.specj.internal.Activator;
public class SpecJMain {
public static void main(String[] args) {
Activator.getDefault().showView();
}
}
What is causing the error, and how can I fix it?
Edit: Adding launch code
Launch Configuration Delegate
public class SpecJLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate {
private static final int UNITS_OF_WORK = 4;
#Override
public void launch(final ILaunchConfiguration configuration, final String mode, final ILaunch launch,
final IProgressMonitor monitor) throws CoreException {
final IProgressMonitor pm = monitor != null ? monitor : new NullProgressMonitor();
pm.beginTask(configuration.getName(), UNITS_OF_WORK);
if (pm.isCanceled()) return;
try {
preLaunchCheck(configuration, mode, pm);
if (pm.isCanceled()) return;
final VMRunnerConfiguration runConfig =
new VMRunnerConfigurationFactory().create(configuration, monitor, this);
if (pm.isCanceled()) return;
getVMRunner(configuration, mode).run(runConfig, launch, pm);
if (pm.isCanceled()) return;
} finally {
pm.done();
}
}
#Override
public String[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
final String[] initialClasspath = super.getClasspath(configuration);
final String pluginClasspath = new SpecJClasspathLocator().getClasspath();
System.out.println(pluginClasspath);
if (pluginClasspath == null) return initialClasspath;
final String[] classpath = new String[initialClasspath.length + 1];
classpath[0] = pluginClasspath;
System.arraycopy(initialClasspath, 0, classpath, 1, initialClasspath.length);
return classpath;
}
}
VMRunnerConfigurationFactory
public class VMRunnerConfigurationFactory {
public VMRunnerConfiguration create(final ILaunchConfiguration configuration, final IProgressMonitor monitor,
final AbstractJavaLaunchConfigurationDelegate delegate) throws CoreException {
final ExecutionArguments execArgs = new ExecutionArguments(delegate.getVMArguments(configuration), "");
// Activator.MAIN_TYPE_NAME is the main class above
final VMRunnerConfiguration runConfig = new VMRunnerConfiguration(Activator.MAIN_TYPE_NAME,
delegate.getClasspath(configuration));
runConfig.setBootClassPath(delegate.getBootpath(configuration));
runConfig.setEnvironment(delegate.getEnvironment(configuration));
runConfig.setProgramArguments(new ProgramArgumentsFactory().create(configuration, monitor));
runConfig.setVMArguments(execArgs.getVMArgumentsArray());
runConfig.setVMSpecificAttributesMap(delegate.getVMSpecificAttributesMap(configuration));
runConfig.setWorkingDirectory(getWorkingDirectoryName(delegate.verifyWorkingDirectory(configuration)));
return runConfig;
}
private String getWorkingDirectoryName(final File workingDir) throws CoreException {
return workingDir != null ? workingDir.getAbsolutePath() : null;
}
}
AbstractUIPlugin is in the org.eclipse.ui.workbench plug-in so you need that in your dependencies.
Edit.
Just noticed you are using a main method - this an Eclipse plugin you can't run this is as a normal Java program. You must either run it in an existing Eclipse RCP or create a RCP.
Edit 2
Your launch does nothing to properly initialize Eclipse to run a plugin. You must start an Eclipse app through the org.eclipse.equinox.launcher.Main entry point (normally via the eclipse executable). You must specify an application or product to run (like in the Run Configuration for an Eclipse Application).
I'm creating a Java RMI program, but I'm getting a ClassNotFoundException.
Could you guys help me figure it out? I'm using Eclipse.
Somebody suggested me it was a codebase problem, but how does this relate?
Here are the codes for my Server and Client:
Server:
package server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import base.Server;
import base.RmiStarter;
public class ServerImplStarter extends RmiStarter{
public ServerImplStarter() {
super(Server.class);
}
#Override
public void doCustomRmiHandling() {
try{
Server engine = new ServerImpl();
Server engineStub = (Server)UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.createRegistry( 1099 );
registry = LocateRegistry.getRegistry();
registry.rebind("Server", engineStub);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new ServerImplStarter();
}
}
Client:
package client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import base.RmiStarter;
import base.Server;
import base.Cell;
public class CellClient extends RmiStarter {
public CellClient() {
super(Server.class);
}
#Override
public void doCustomRmiHandling() {
try{
Registry registry = LocateRegistry.getRegistry();
Server server = (Server)registry.lookup("Server");
Cell c = null;
c = server.getcell();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new CellClient();
}
}
and the error is this:
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.CellImpl
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.getcell(Unknown Source)
at client.CellClient.doCustomRmiHandling(CellClient.java:23)
at base.RmiStarter.<init>(RmiStarter.java:19)
at client.CellClient.<init>(CellClient.java:13)
at client.CellClient.main(CellClient.java:31)
Caused by: java.lang.ClassNotFoundException: server.CellImpl
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source)
... 8 more
Both client and server should have same package name. I had the same error yesterday, i corrected it after lots of searching. Try it and tell me if any other error comes.
Mark this as answer, if you find so.Thank You !!
CellImpl has not been exported or made available via the client's CLASSPATH. It needs to either (a) extend UnicastRemoteObject or be exported via UnicastRemoteObject.exportObject(); or (b) be Serializable and available on the client's CLASSPATH.
Solution for NoClassFound Exception when running RMI Client.
Case:
The server and client files are in different folders.
Example:
Server side: The server interface and server implementation are inside
Project Folder: C:\RMIServer
Package: rmiserver
Client side: The server interface and the client are inside
Project Folder: C:\RMIClient
Package: rmiClient
Problem: Could not find the location of the server interface.
Solution: In the client side,
1. Create a package name rmiServer (the package name should be the same as the server side package).
2. Place the server stub inside this package.
3. The client.java is inside the rmiClient package. Now import the rmiServer package in the client.java file.
import rmiServer.*;
This will solve the classNotFound Exception that happens when we execute the client side RMI.
Today I started coding a project that uses sqlite and when I tried to test it I received the java.lang.ClassFormatError: Invalid pc in LineNumberTable. Hope you can help me with it, because I'm just lost. I'v searched about this error, and came across some weird solutions to some weird causes. As I understand, none of them was causing my problem.
I'm using "sqlitejdbc-v056" wich is inserted in my classpath.
Edit: My JDK version is 1.7.0_03-b05
StackTrace:
Exception in thread "main" java.lang.ClassFormatError: Invalid pc in LineNumberTable in class file Controllers/FuncionariosController
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at Main.main(Main.java:13)
Code giving the error:
public class Main {
public static void main(String[] args) {
DatabaseController db=new DatabaseController();
FuncionariosController f=new FuncionariosController(db);
...
}
}
Class FuncionarioController:
package Controllers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Models.Funcionario;
public class FuncionariosController {
private DatabaseController db;
public FuncionariosController(DatabaseController db){
this.db=db;
}
public void render(String cargo){
...
}
public List<Funcionario> selectAll(){
...
}
public Funcionario select(String login){
...
}
}
Thanks in advance,
I had a simmilar problem.
It occured that it was caused by switch that haven't got cases code.
While the error occured it looked somtheing like this:
switch(e.getActionCommand()) {
case ACTION_OPEN_PID:
break;
case ACTION_OPEN_PPID:
break;
default:
}
After adding simple console output to the cases the error was gone.
switch(e.getActionCommand()) {
case ACTION_OPEN_PID:
System.out.println(ACTION_OPEN_PID);
break;
case ACTION_OPEN_PPID:
System.out.println(ACTION_OPEN_PPID);
break;
default:
}
Thanks to Luiggi Mendoza I solved it by using JDK 1.6 instead of 1.7. That did the trick, but I had to comment out a lot of code to compile it. Not that good a solution....
I'm working though a book SWT/JFace IN ACTION by Manning Press.
When I added JFace, Eclipse for some reason could not find the main class though it is plainly present.
Here is the code
package com.swtjface.ChTwo;
import org.eclipse.jface.window.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class HelloSWT_JFace extends ApplicationWindow{
/**
* #param args
*/
public HelloSWT_JFace(){
super(null);
}
protected Control createContents(Composite parent){
Text helloText = new Text(parent, SWT.CENTER);
helloText.setText("Hello SWT and JFace!");
parent.pack();
return parent;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloSWT_JFace awin = new HelloSWT_JFace();
awin.setBlockOnOpen(true);
awin.open();
Display.getCurrent().dispose();
}
}
The reject message I get from Eclipse is...
Could not find the main class: com.swtjface.ChTwo.HelloSWT_JFace.
Program will exit.
Here is the exception...
java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IProgressMonitor
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.IProgressMonitor
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"
You need at least 2 more jars to use JFace:
org.eclipse.equinox.common
org.eclipse.core.commands
See Using JFace outside the Eclipse platform for more details.
You need to add jar file for "org.eclipse.core.runtime.IProgressMonitor class"
Check this link.