Unable to run Selenium tests (java) from the command line - java

I am using Selenium to do tests on some html. I was using java under Eclipse and run test by Junit. And everything was fine. But I got problems when I started use command line (javac/java at Dos prompt) to compile/run the test.
For this java project, I have a bunch of classes, each one represents one test while each each class sits in its own java file. And All these files are under one package/folder called "testSuite". Each test file will import a bunch of jar files. And they also import Global class from another
The problems, the compile was ok. but I make it run. I compiled global.java first then compiled PolicyFilter.java and then try to run the class, below is what I got:
Exception in thread "main" java.lang.NoClassDefFoundError: \testsuite\PolicyFilter (wrong name: testSuite/PolicyFilter)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)

Please use java -cp requiredClassfiles/folders/jars classToExecute.
I hope you have Selenium RC Jar available and started it using command java -jar selenium-server-standalone-<version-number>.jar.

Make sure that you put the annotation #Test on the test, clean the folder target in project folder and then restart the eclipse.
Hope it works :))

Related

Run class main from groovy script using grape

I would like to run the main method of a java class by using #Grab so that requirements are taken care of automatically. More specifically I would like to run the pdfbox example https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
I wrote the following groovy script
#!/usr/bin/env groovy
#Grab('org.apache.pdfbox:pdfbox-examples:2.0.20')
import org.apache.pdfbox.examples.util.RemoveAllText
RemoveAllText.main(args)
The #Grab, import and execution of main seems to work. But the main seems to recall itself repeatedly thus failing with a StackOverflowError as below.
Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
at RemoveAllText.main(RemoveAllText.groovy)
at RemoveAllText$main.call(Unknown Source)
at RemoveAllText.run(RemoveAllText.groovy:5)
at RemoveAllText.main(RemoveAllText.groovy)
...
I am new to groovy so I am not sure what I am doing wrong. Is what I am trying to do possible? If it is possible, how would it be done?
To make the example fully reproducible I get the above error when I use the pdf found at https://github.com/mozilla/pdf.js/raw/v2.4.456/examples/learning/helloworld.pdf and using groovy version 2.4.16 installed using the default repositories in Ubuntu 18.04. The command run would be
groovy RemoveAllText.groovy helloworld.pdf helloworld_out.pdf
If I manually download the required jar files and I run
java -cp pdfbox-2.0.20.jar:commons-logging-1.2.jar:pdfbox-examples-2.0.20.jar org.apache.pdfbox.examples.util.RemoveAllText helloworld.pdf helloworld_out.pdf
it works without problem.
Rename your script from RemoveAllText.groovy to something else and everything should be fine.
Problem that your groovy script produces the same class name as Apache class.

Exception in thread "main" Error in Eclipse when trying to run TestNG class

When I attempt to run a sample TestNG class in Eclipse with Java 1.7.0_79 I get the following errror:
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -protocol
at com.beust.jcommander.JCommander.parseValues(JCommander.java:742)
at com.beust.jcommander.JCommander.parse(JCommander.java:282)
at com.beust.jcommander.JCommander.parse(JCommander.java:265)
at com.beust.jcommander.JCommander.<init>(JCommander.java:210)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:162
I have tried installing TestNG through the Eclipse store, when that didn't work. I uninstalled and did it through the www.beust.com/eclipse site.
My class doesn't show any errors, but my project does have a question mark in the lower-left part of the image.
I thought it was because I was missing the jcommander.jar. So I've even gone and tracked that down and included it in my libraries.
The weird thing is, is that I am running the same configurations on my Windows PC and was able to get TestNG scripts running, but when I go through the same setup on my Mac, I get the error.
This is the code I'm trying to run:
import org.testng.annotations.Test;
public class NewTest {
#Test
public void testMethod() {
System.out.println("First TestNG test");
}
}
Make sure you added the jcommander.jar file to your classpath if you are using a project without maven. Make sure your project structure has a "TestNG" library that has only one testng.jar file inside it.

Fail to run the demo of this java application

I am completely new to Java, just learned a little because I need to run this project: https://github.com/ansjsun/ansj_seg
I have run
mvn compile
and some other stuff.
Now, in ansj_seg/target I have a file named ansj_seg-0.8.jar, which seems important, although I don't know how to use it.
In ansj_seg/src/test/java/org/ansj/demo, there are some demos, and I want to run BaseAnalysisDemo.java, I tried as following.
Step one, I compile using:
javac -classpath ~/Downloads/ansj_seg/target/ansj_seg-0.8.jar BaseAnalysisDemo.java
It works fine and generates BaseAnalysisDemo.class.
But when I try to run it using
java BaseAnalysisDemo
An error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: BaseAnalysisDemo (wrong name: org/ansj/demo/BaseAnalysisDemo)
I guess I should set some path, but have no idea.
Anyone can help?
BTW, I prefer using command line to Eclipse.
The directory structure is like this(Updated):
\ansj_seg
pom.xml
\src
\main
\java
\org
\ansj
\app
\dic
\domain
...
\resource
...
\test
\java
\org
\ansj
\demo
BaseAnalysisDemo.java
Demo.java
...
\test
...
Here is another question, even if the demo can be run, how can I use this java library in another place? I guess the file ansj_set-0.8.jar should be used. Again, I know nothing about Java.. Any suggestion will be very helpful.
Updated:
If I run with classpath specified:
java -classpath ~/Downloads/ansj_seg/target/ansj_seg-0.8.jar BaseAnalysisDemo
still got an error, but different:
Exception in thread "main" java.lang.NoClassDefFoundError: BaseAnalysisDemo
You will fall back to Eclipse soon, because what you'd like to do is a bit cumbersome, but doable.
Try this:
mvn exec:java -Dexec.mainClass="java.org.ansj.demo.BaseAnalysisDemo.java"
If you need arguments too, add -Dexec.args to the list.
Note that you'll have to move your demo under the main source tree.

How to use 'java' command options/arguments when executing JUnit tests

I'm developing a C# application to run JUnit tests automatically. Since it's complicated to move the current location where my C# application is running, i need to execute the JUnit tests without directly go inside their folder.
So, i have this folder tree:
» ProjectFolder
»» ClassesFolder
»» TestsFolder
»»» testX.java
»»» testX.class
I'm already able to compile the files, using:
javac ...\ProjectFolder\ClassesFolder\*.java
javac ...\ProjectFolder\TestsFolder\*.java
But i can't execute the tests. I tried:
java -classpath ...\ProjectFolder org.junit.runner.JUnitCore TestsFolder.testX
And got this error:
Error: Could not find or load main class org.junit.runner.JUnitCore
I even try this way:
java org.junit.runner.JUnitCore ...\ProjectFolder TestsFolder.testX
But, once again, it doesn't work:
JUnit version 4.10
Could not find class: -classpath
Could not find class: ...\ProjectFolder
Could not find class: TestsFolder.testX
Time: 0,003
OK (0 tests)
So, my main doubt, is how i can use the options (like -cp or -classpath) of the java command, when executing JUnit tests using org.junit.runner.JUnitCore.
Thanks in advance.
The problem is that the jUnit-runner is not in the classpath you specify. You need to add the path to junit.jar to the classpath.
Google is nice http://www.jsystemtest.org/?q=node/44
But why not just use something that already exists and can take care of this? Like Maven or Ant?

Classpath compiles correctly, but will not run. What am I missing

Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
My guess is that . is not on your classpath. For example, you might be compiling with:
javac -cp foo.jar:bar.jar Test.java
but then to run the code you'd need
java -cp foo.jar:bar.jar:. Test
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.
Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.

Categories

Resources