I am doing a project in IntelliJ IDEA on JavaFX application for the first time. I am unable to run my code.
When I first build->Run, I got an error saying that I require kotlin stdlib in my module,then I downloaded and added kotlin standard library into my module.
But now I am getting java.lang.NoClassDefFoundError in my code .
Sample.Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
module-info.java file
module Calculator {
requires javafx.fxml;
requires javafx.controls;
requires kotlin.stdlib;
opens Sample;
}
fxml file
<GridPane fx:controller="Sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>
error message:
"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" "-javaagent:C:\Coding_Setups\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\lib\idea_rt.jar=52562:C:\Coding_Setups\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "G:\JavaFXproject\Calculator\out\production\Calculator;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.swing.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.web.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.media.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.graphics.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.fxml.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.controls.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Coding_Setups\javafx lib\javafx-sdk-11.0.2\lib\javafx-swt.jar" sample.Main
Error: Could not find or load main class sample.Main
Caused by: java.lang.NoClassDefFoundError: Sample/Main (wrong name: sample/Main)
I am confused why would my javafx project demand for kotlin stdlib and why I have added it to my module I am getting a new error even when I have not changed the provided code yet.
Related
This is my script:
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.close();
}
}
When I run the script, am getting below error, I tried to add the java lib in Class path instead of Module path, still the issue not resolved, someone please help;
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type FirefoxDriver cannot be resolved to a type at sampleTests.AdminLogin.main(AdminLogin.java:10)
You have to set the path of the Geckodriver (the executable that is used for Firefox tests using Selenium).
Search for Geckodriver in Google.
Download the executable as per your system (Win/Mac/Linux)
Set the path of the executable in your tests as given below -
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",Path_of_Firefox_Driver");
// example for Windows
System.setProperty("webdriver.gecko.driver",D:\\GeckoDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://testautomationu.applitools.com/");
driver.quit();
}
}
I have a problem with TestNG. I cannot run a test.
I am getting this error:
POM.xml has no errors.
Here is the code in test page:
import Pages.SearchPage;
import org.testng.annotations.Test;
import core.Web.AllListeners.*;
public class Search extends Listener {
#Test(groups = "Regression")
public void ticketBookingFunctionality() {
new SearchPage()
.openUrl()
.inputCaption("Comic")
.selectCityByValue()
.inputDateFrom("2020-01-01")
.inputDateTo("2021-07-05")
.clickButtonSearch()
.clickButtonBuy()
.chooseTicket()
.choosePrice()
.pushButtonFindTickets()
.closeLoginPopup();
}
}
Where can be the problem?
You need to add the following testng related jar files within your project.
My cucumber runner class cannot seem to find my step definition class, but it finds the feature file just fine.
Here is a snapshot of my very simple project structure:
Feature file:
Feature: Customer must be able to reach the Home Delivery page by clicking the Home Delivery button
Scenario: Test Home Delivery button
Given PS Restaurants web page is open
When User clicks on Home Delivery button
Then Home Delivery page should load
Step Definition class:
package stepDefinitions;
import java.util.concurrent.TimeUnit;
public class E_1_US_1 {
#Given("^PS Restaurants web page is open$")
public void ps_Restaurants_web_page_is_open() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^User clicks on Home Delivery button$")
public void user_clicks_on_Home_Delivery_button() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^Home Delivery page should load$")
public void home_Delivery_page_should_load() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
}
Runner class:
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/features/Feature_1_1.feature"},
glue= {"src/stepDefinitions/E_1_US_1.java"}
)
public class Runner {
}
The output from running the Runner class as a JUnit test:
1 Scenarios ([33m1 undefined[0m)
3 Steps ([33m3 undefined[0m)
0m0.040s
You can implement missing steps with the snippets below:
#Given("^PS Restaurants web page is open$")
public void ps_Restaurants_web_page_is_open() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^User clicks on Home Delivery button$")
public void user_clicks_on_Home_Delivery_button() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^Home Delivery page should load$")
public void home_Delivery_page_should_load() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
I just copied and pasted the output into my step definitions class and it still won't recognize the step definitions class. Earlier it was saying that there was no matching glue code for each step in the scenario, but that went away after I closed and reopened the feature file.
You can update your runner class as below:
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/features"}, //It will pick all the feature files located in this folder
glue= {"stepDefinitions"} //Only package name is required here.
)
public class Runner {
}
I have a cucumber project and I want to get al the tags in the project to be able to choose them as parameters.
I found this question where cucumber had an option to get the tags but I found that doesn't work anymore, then I found this other question where I found I need a custom formatter to get my tags, but it is for ruby, and I need it for Java, so then I found this article on how to create a custom formatter, but I found that this worked for the cukes version and I'm using the io one.
So I searched inside the cucumber packages and created a custom formatter from a copy of the JSONFormatter inside the package cucumber.runtime.formatter, here is my code:
import cucumber.api.TestCase;
import cucumber.api.event.*;
import cucumber.api.formatter.Formatter;
import cucumber.api.formatter.NiceAppendable;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.pickles.PickleTag;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TagsFormatter implements Formatter {
private String currentFeatureFile;
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final NiceAppendable out;
private List<String> tags = new ArrayList<>();
private EventHandler<TestCaseStarted> caseStartedHandler = this::handleTestCaseStarted;
private EventHandler<TestRunFinished> runFinishedHandler = event -> finishReport();
public TagsFormatter(Appendable out) {
this.out = new NiceAppendable(out);
}
#Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
publisher.registerHandlerFor(TestRunFinished.class, runFinishedHandler);
}
private void handleTestCaseStarted(TestCaseStarted event) {
if (currentFeatureFile == null || !currentFeatureFile.equals(event.testCase.getUri())) {
currentFeatureFile = event.testCase.getUri();
collectTags(event.testCase);
}
}
private void finishReport() {
out.append(gson.toJson(tags));
out.close();
}
private void collectTags(TestCase testCase) {
testCase.getTags();
tags.addAll(testCase.getTags()
.stream()
.map(PickleTag::getName)
.collect(Collectors.toList()));
}
}
I copied the libraries I need to run cucumber in a lib folder inside my project and tried running it using my formatter like this:
java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
But Im getting a class not found exception:
λ java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
Exception in thread "main" cucumber.runtime.CucumberException: Couldn't load plugin class: com.myproject.formatters.TagsFormatter
at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:181)
at cucumber.runtime.formatter.PluginFactory.pluginClass(PluginFactory.java:166)
at cucumber.runtime.formatter.PluginFactory.getPluginClass(PluginFactory.java:223)
at cucumber.runtime.formatter.PluginFactory.isFormatterName(PluginFactory.java:201)
at cucumber.runtime.RuntimeOptions$ParsedPluginData.addPluginName(RuntimeOptions.java:471)
at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:157)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:115)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:108)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:100)
at cucumber.api.cli.Main.run(Main.java:31)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: com.myproject.formatters.TagsFormatter
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 cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:174)
... 10 more
So, how can I create this formatter in a way it is recognized? or at least get the tags list from cucumber from console?
Thanks
By just eyeballing your code, I don't think there is anything wrong with it. However your command does not appear to include a compiled version of the TagsFormatter on the class path.
If your compiled sources are in .\bin\ make sure to include that folder ie:
java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar;.\bin\* cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
I added an external jar in my eclipse dynamic webproject via Folder -> properties -> build path -> Libraries -> add external jar.
The code is working fine on compile time.
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tika.Tika;
#WebServlet(name="UploadServlet", urlPatterns={"/uploadFile"}) // specify urlPattern for servlet
#MultipartConfig //Specify that this servlet will receive a multipart data
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
Tika tika = new Tika();
res.setContentType("text/html");
PrintWriter writer = res.getWriter();
writer.write(mediaType);
}
}
I added Apache Tika but when I run my application. these exception occured.
root cause
java.lang.NoClassDefFoundError: org/apache/tika/Tika
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.ClassNotFoundException: org.apache.tika.Tika
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
I am using Apache Tika to determine what file has been upload, I want to use it for validating if a file is an image, or audio file
You forgot to add that jar to the Deployment Assembly page. It's not deployed to the server when you run your application, hence the NoClassDefFoundErrors.