NullPointerException in InitialContext - glassfish

NullPointerException in InitialContext
private void connect() {
try {
InitialContext ctx = new InitialContext();
IServerBean serverBean = (IServerBean)ctx.
lookup("java:global/applicationserver/ServerBean!
com.test.applicationserver.IServerBean");
} catch (NamingException e) {
logger.error(e.getMessage(), e);
}
}
I got this exception when try to execute client application through console java -cp cleint-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.test.client.EJBClient:
java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:335)
at com.sun.enterprise.naming.impl.SerialInitContextFactory.createInitialContext
(SerialInitContextFactory.java:358)
at com.sun.enterprise.naming.impl.SerialInitContextFactory.getInitialContext
(SerialInitContextFactory.java:353)
at com.sun.enterprise.naming.SerialInitContextFactory.getInitialContext
(SerialInitContextFactory.java:69)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at com.test.client.EJBClient.connect(EJBClient.java:33)
at com.test.client.EJBClient.main(EJBClient.java:61)
This application connecting to remote EJB module.
But when I try to execute it in Eclipse, all's gone fine.
My configuration:
Java SE 1.7
GlassFish Server Open Source Edition 3.1.2.2 (build 5)
Eclipse Java EE IDE for Web Developers. Version: Juno Service Release
1 Build id: 20121004-1855
Maven dependencies:
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>C:\Java\jdk1.7.0_11\lib\tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>javaee</artifactId>
<version>3.1.2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.appclient</groupId>
<artifactId>gf-client</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>

I had the same problem.
Resolved :
You must add gf-client.jar and appserv-rt.jar not only in classpath of your project but also in runtime (With Eclipse, See Run As > Run configurations > Classpath > Add jars ) .

I solved this problem with this solution:
Adding this libraries to folder with application:
glassfish-embedded-all-3.1.1.jar hk2-core-1.6.9.jar
internal-api-3.1.2.2.jar
common-util-3.1.2.2.jar
glassfish-corba-internal-api-3.2.0-b005.jar
glassfish-naming-3.1.2.2.jar
And when running application, add this libraries to classpath:
java -cp glassfish-embedded.jar;hk2-core.jar;internal-api.jar;common-util.jar;glassfish-corba-internal-api.jar;glassfish-naming.jar;cleintconsole-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.test.client.EJBClient

Related

Kotlin noArg plugin not recognised by IntelliJ

I am not sure if I've missed a config or stumbled upon a bug. I am using IntelliJ to build a Kotlin Spring Boot application with JPA and would like to use Kotlin noArg plugin to reduce boilerplate in Entities.
With the build.gradle.kts below my application compiles OK but IntellJ underlines my Entity with error Class 'User' should have [public, protected] no-arg constructor.
Is there something I can set up in IntelliJ or build.gradle to make the error disappear?
System specs:
Windows 11 + WSL
IntelliJ IDEA 2021.3.1 (Ultimate Edition)
Kotlin plugin 213-1.6.10-release-944-IJ6461.79
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.6.4"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id("org.jetbrains.kotlin.plugin.noarg") version "1.6.10"
kotlin("jvm") version "1.6.10"
kotlin("plugin.spring") version "1.6.10"
kotlin("plugin.jpa") version "1.6.10"
}
group = "XXXXXX"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_16
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
}
noArg {
annotation("javax.persistence.Entity")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "16"
}
}
I had the same problem, but with my Maven project. The thing was in <executions> block. Maybe IDEA doesn't understand such complex configs yet. I removed it and kept this simple config.
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
<compilerPlugins>
<plugin>jpa</plugin>
<plugin>all-open</plugin>
<plugin>spring</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=javax.persistence.Entity</option>
<option>all-open:annotation=javax.persistence.Embeddable</option>
<option>all-open:annotation=javax.persistence.MappedSuperclass</option>
</pluginOptions>
<args>
<!-- https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-kotlin.html#boot-features-kotlin-null-safety -->
<arg>-Xjsr305=strict</arg>
</args>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
Turns out that it's a bug in IntelliJ running on WSL-based project.
I'm tracking the bug here https://youtrack.jetbrains.com/issue/KTIJ-21314/ and will update the answer once resolved.
You just need to reload the project, for me it worked in a maven project.

junit5 with selenium testcontainer not working

Goal - I want to be able to write selenium tests, using Junit 5, and testcontainers. I am writing a very simple code to just be able to check an attribute of the search bar of google's homepage.
Issue - chrome.getWebDriver(); returns null. Am I missing something?
exception - java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.manage()" because "driver" is null.
this is caused as I try to set an implicit wait after initializing WebDriver.
my pom.xml -
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<test-containers.version>1.16.0</test-containers.version>
<selenium.version>3.141.59</selenium.version>
<junit.version>5.8.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>selenium</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
My test class -
#TestInstance(Lifecycle.PER_CLASS)
#Testcontainers
public class GoogleTest {
#Container
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withCapabilities(Config.getChromeOptions());
private GooglePage googlePage;
#BeforeAll
public void setup() throws Exception {
// ISSUE IS WITH THE LINE BELOW
WebDriver driver = chrome.getWebDriver();
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
// trying to use page object model, just navigates to the google homepage
googlePage = new GooglePage(driver);
}
#Test
public void testTitle() throws Exception {
WebElement element = googlePage.getSearchBar();
Thread.sleep(5000);
assertEquals(element.getAttribute("role"), "combobox");
}
}
In case you need it, the google drive link for the code
got my mistake, if using junit5, fields should be private static final I think...
I was still using public...

Unable to set maven dependencies for RemoteWebDriver

I am not able to resolve RemoteWebDriver in eclipse due issues in maven dependency. I need RemoteWebDriver to get the browser version (for reporting purpose). I have mentioned the following maven dependencies yet I am not able to resolve RemoteWebDriver. As per the earlier post The import org.openqa.selenium.remote.CapabilityType cannot be resolved I have to manually download selenium-standalone-server. I am not understanding as to why maven dependency is not sufficient? Is there any other maven dependency that can be added to resolve RemoteWebDriver
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version>
</dependency>
*************************
//Below is what I am trying to code
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
It is quite enough to have only selenium-java, it will resolve selenium-remote-driver via Maven transitive dependency mechanism
Given you mention that you have to manually download Selenium Standalone Server you don't need this selenium-server dependency as well
So it should be as simple as:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-java</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
Test class:
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class SeleniumTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "c:/apps/webdriver/chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Capabilities cap = driver.getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);
driver.quit();
}
}
Demo:
More information:
Remote WebDriver
Selenium with Java
ChromeDriver - Getting Started

Error running Spark Main Class - Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Handler

I am learning Mongodb for Java and I am trying to run the code below which shows jetty handler error. I tried searching for solutions in different threads of this forum but none of the solutions helps me. Please help. What am I missing?
package com.mongo.practice;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;
public class SparkMain {
public static void main(String[] args){
Spark.get("/", new Route() {
public Object handle(final Request request, final Response response){
return "Hello worldfrom Spark";
}
});
}
}
I get this error when run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Handler
at spark.embeddedserver.EmbeddedServers.initialize(EmbeddedServers.java:40)
at spark.Service.lambda$init$2(Service.java:536)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mongo</groupId>
<artifactId>M101J</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
</project>
I tried adding this
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.9.v20180320</version>
</dependency>
but it did not solve the issue
You have to add two dependencies. The following:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-util -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.3.24.v20180605</version>
</dependency>
The version for sl4j can varry. But the version of jetty is not flexible. I have tried with the latest version. It does not work for any other version other than this.

Weblogic Jaxws deployment - class does not support JDK1.5

WebLogic Server Version: 10.3.6.0
Spring version: 3.2.1.RELEASE
Java JDK 1.6
I am trying to deploy a Spring application as WAR that uses jaxws into a Weblogic server.
The application works well with Jetty. However when deploying(I mean starting deployed app) Weblogic following exception occurs:
Caused By: java.lang.UnsupportedOperationException: This class does not support JDK1.5
at weblogic.xml.jaxp.RegistryTransformerFactory.setFeature(RegistryTransformerFactory.java:317)
at com.sun.xml.ws.util.xml.XmlUtil.newTransformerFactory(XmlUtil.java:392)
at com.sun.xml.ws.util.xml.XmlUtil.newTransformerFactory(XmlUtil.java:400)
at com.sun.xml.ws.util.xml.XmlUtil.<clinit>(XmlUtil.java:233)
at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:36
.
maven pom.xml
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.jvnet.jax-ws-commons.spring</groupId>
<artifactId>jaxws-spring</artifactId>
<version>1.9</version>
</dependency>
Weblogic.xml
<weblogic-web-app>
<context-root>/MyApp</context-root>
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
<show-archived-real-path-enabled>true</show-archived-real-path-enabled>
</container-descriptor>
</weblogic-web-app>
It is being fixed by changing weblogic.xml
<container-descriptor>
<prefer-web-inf-classes>false</prefer-web-inf-classes>
<show-archived-real-path-enabled>true</show-archived-real-path-enabled>
<prefer-application-packages>
<package-name>com.sun.xml.ws.server.*</package-name>
</prefer-application-packages>
</container-descriptor>
And in init servlet (if you use the old style) you should change the way you acquire the context as:
private static WebApplicationContext context;
#Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
this.context = WebApplicationContextUtils.getWebApplicationContext(sc);
...
}
public static WebApplicationContext getApplicationContext(){
return context;
}
That fixes it