Getting Writer error on using TinyLog - tinylog

I tried using TinyLog for a sample application to make sure understanding how it works, so that i can use it for my work application.
But, while using i get an exception to type cast the Writer (Compile time error). If i typecast it, it throws runtime error as below.
Code :
import java.io.FileWriter;
import java.io.IOException;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.Writer;
public class TestClass {
public static void main(String[] args) throws IOException {
Configurator.defaultConfig().writer(new ConsoleWriter()).addWriter((Writer) new FileWriter("data.txt")).activate();
Logger.info("welcome to tinylog logger.....");
}
}
Error :-
Exception in thread "main" java.lang.ClassCastException: java.io.FileWriter cannot be cast to org.pmw.tinylog.writers.Writer
at TestClass.main(TestClass.java:12)
Please help us :).

You can fix it easily. Just import org.pmw.tinylog.writers.FileWriter instead of java.io.FileWriter. The class java.io.FileWriter is the file writer of the JVM, but you need tinylog's file writer.
Afterwards you can remove the class cast "(Writer)" as org.pmw.tinylog.writers.FileWriter is an implementation of the interface org.pmw.tinylog.writers.Writer. However the class java.io.FileWriter does not implement the interface org.pmw.tinylog.writers.Writer. Therefor the reported ClassCastException has been thrown.
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.FileWriter;
public class TestClass {
public static void main(String[] args) {
Configurator.defaultConfig().writer(new ConsoleWriter()).addWriter(new FileWriter("data.txt")).activate();
Logger.info("welcome to tinylog logger.....");
}
}

Related

Selenium Jar Files for Eclipse

I am trying to run the below code
package JavaBasics;
enter code here
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenBrowserURL {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Pooja\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.makemytrip.com/?ccde=us");
}
}
I am getting error java.lang.NoClassDefFoundError for get as the jar files that i downloaded 3.141.59 has get as void
How can i fix this? Is this an issue with the jar files I downloaded. Please advice

How to resolve this error on java with selenium?

//Eclipse java code:
package openbrowser;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenChrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
WebDriver driver=new ChromeDriver();
}
}
eclipse error
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Om Murugaa\Downloads\Testing\selenium-server-standalone-3.141.59.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.eclipse.jetty.http.Http1FieldPreEncoder not in module
Adding maven into eclipse resolved this error.

Fetch all Form tags from HTML from console in eclipse selenium java

I got the HTML in console using getpagesource in selenium java .
Now i need only the tag 'Forms' in the console result.
How do i do that?
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://''/tandem/login/?");
String pagesource = driver.getPageSource();
System.out.println(pagesource);
}
You can use following code snippet for this :
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//Sample Java class to fetch list of all tag with tagname forms
public class Test {
public static void main(String[] args) {
String geckoDriverPath=Class1.class.getResource("/firefox/geckodriver.exe").getPath();
System.setProperty("webdriver.gecko.driver",geckoDriverPath);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.URL.com");
List<WebElement>list =driver.findElements(By.tagName("form"));
for(WebElement ele:list) {
System.out.println(ele.getText());
}
}
}
Once you get the lists you can fetch individual form element and perform action like getting text.Hope, this helps

Hibernate and Eclipse error

I'm creating a simple web application with a register form, and I have used Hibernate. But i have this kind of error:
this is the code:
AddUser.java
import java.io.IOException;
import javax.imageio.spi.ServiceRegistry;
import javax.security.auth.login.Configuration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
#WebServlet("/CreateUser.do")
public class AddUser extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddUser() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory factory = config.buildSessionFactory(servReg);
Session session = factory.openSession();
session.beginTransaction();
User u = new User(request.getParameter("firstname"), request.getParameter("lastname"), request.getParameter("login"), request.getParameter("password"));
session.save(u);
session.getTransaction().commit();
session.close();
RequestDispatcher view = request.getRequestDispatcher("useradd.jsp");
view.forward(request, response);
}
}
This is the error: ERROR
How can I do to solve this problem? Thanks you!
#LuisMuñoz
I add also the error if someone want to see it directly.
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Configuration
The method configure() is undefined for the type Configuration
The method getProperties() is undefined for the type Configuration
The method buildSessionFactory(ServiceRegistry) is undefined for the type Configuration
AddUser.doPost(AddUser.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
My Eclipse Page:
ECLIPSE
From the error it looks like there is no method configure for Configuration object. Check the javadocs https://docs.oracle.com/javase/9/docs/api/index.html?javax/security/auth/login/Configuration.html

Error: Main method not found in class newpackage.Myclass, please define the main method as: public static void main(String[] args)

Trace:
Error: Main method not found in class newpackage.Myclass, please
define the main methods: public static void main(String[] args) or a
JavaFX application class must extend javafx.application.Application.
MyClass.java
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import javafx.application.*;
public class Myclass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users \\karokiaswamy\\Documents\\Selenium_installation\\geckodriver-v0.15.0-win64)\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://Application URL");
driver.findElement(By.xpath("//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[1]/input")).sendKeys("USER NAME");
driver.findElement(By.xpath("//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[2]/input")).sendKeys("Password");
driver.findElement(By.xpath("//*[#id='siteLoginTab]/form/table/tbody/tr[1]/td[3]/input")).click();
driver.findElement(By.xpath(".//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[2]/input")).sendKeys(" ");
driver.findElement(By.xpath(".//*[#id='siteMain']/div[1]/div/form/table/tbody/tr[3]/td/input")).click();
driver.findElement(By.xpath(".//*[#id='siteMain']/div[1]/div/form/table/tbody/tr[3]/td/input")).click();
}
}