import of java.lang.String fails in JAVAFX 11 FXML - javafx

I have an JAVAFX 11 FXML file describing a popup window I use in an application. After refactoring the project structure where I moved the FXML from the "app" project to another project, I get NullPointerException errors, because the XML loader fails to import java.lang.String (and others)
The FXML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<TitledPane fx:id="GeneticMain"
animated="false"
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="400.0"
text="Genetic Scanner Config"
xmlns="http://javafx.com/javafx/8.0.141"
xmlns:fx="http://javafx.com/fxml/1">
<content>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity">
<children>
<GridPane layoutX="10.0"
...
Bottom of Stack trace:
Caused by: java.lang.NullPointerException
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2931) ~[javafx-fxml-12.0.1-linux.jar:?]
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2920) ~[javafx-fxml-12.0.1-linux.jar:?]
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2861) ~[javafx-fxml-12.0.1-linux.jar:?]
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2707) ~[javafx-fxml-12.0.1-linux.jar:?]
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2676) ~[javafx-fxml-12.0.1-linux.jar:?]
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2542) ~[javafx-fxml-12.0.1-linux.jar:?]
Effective POM-dependencies to Javafx look like this:
<properties>
...
<dependencyversion.openjfx>12.0.1</dependencyversion.openjfx>
...
</properties>
...
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>12.0.2</version>
</dependency>
<dependency>
<groupId>de.gsi.chart</groupId>
<artifactId>chartfx-chart</artifactId>
<version>11.0.3</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<artifactId>javafx-base</artifactId>
<groupId>org.openjfx</groupId>
</exclusion>
<exclusion>
<artifactId>javafx-web</artifactId>
<groupId>org.openjfx</groupId>
</exclusion>
</exclusions>
</dependency>
I'm happy for any suggestion...

Thanks to fabian I am able to provide a solution to my problem.
The root cause of my problem was that my popup window's class loader, which was called in PopupController.showPopupWindow() was null. Initial code:
public void showPopupWindow() throws Exception {
Parent popupWindowRoot = null;
try {
URL url = getClass().getResource(url.to.popupFXML.file);
final FXMLLoader fxmlLoader = new FXMLLoader(url);
fxmlLoader.setController(this);
popupWindowRoot = (Parent) fxmlLoader.load();
} catch (final IOException e) {
MessageLogger.logError(getClass(), e);
throw e;
}
...
}
Which was called via an onAction method in the main window:
private void handle_onAction(event onAction) throws Exception {
popupController = new PopupController();
popupController.setParentController(this);
try {
popupController.showPopupWindow();
} catch (final Exception ex) {
APPLOGGER.error(true, true, "Error caught in method showPopupWindow(): ");
}
}
By following Fabian's suggestion, I passed the stage of the main window to method showPopupWindow and used this stage to set explicitly the classLoader of the popup window. New code:
public void showPopupWindow(Stage parentStage) throws Exception {
Parent popupWindowRoot = null;
try {
URL url = getClass().getResource(url.to.popupFXML.file);
final FXMLLoader fxmlLoader = new FXMLLoader(url);
fxmlLoader.setController(this);
fxmlLoader.setClassLoader(parentStage.getClass().getClassLoader());
popupWindowRoot = (Parent) fxmlLoader.load();
} catch (final IOException e) {
MessageLogger.logError(getClass(), e);
throw e;
}
...
}
The calling method was adjusted accordingly.

Related

Firebase send request with oauth2 token example not working

Trying to get simple google oauth example from Firebase docs to work using Firebase service account json file https://firebase.google.com/docs/cloud-messaging/auth-server#windows :
private static String getAccessToken() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new FileInputStream("service-account.json"))
.createScoped(Arrays.asList(SCOPES));
googleCredentials.refreshAccessToken();
return googleCredentials.getAccessToken().getTokenValue();
} //Messaging.java
Following is my code in Netbeans IDE:
import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
/**
*
* #author Administrator
*/
public class TrOauth2 {
private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
private static final String[] SCOPES = {MESSAGING_SCOPE};
private static String comeon;
public static void main(String[] args) {
try {
System.out.println("Hello World!");
comeon = getAccessToken();
System.out.println(comeon);
} catch (IOException ex) {
Logger.getLogger(TrOauth2.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception when calling getAcessToken();");
}
}
private static String getAccessToken() throws FileNotFoundException, IOException {
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(new FileInputStream("C:\\Users\\administrator\\Downloads\\lora-alarm-firebase-adminsdk-xxx-xxx.json")).createScoped(Arrays.asList(SCOPES));
System.out.println("googleCredentials: " + googleCredentials.toString());
return googleCredentials.getAccessToken().getTokenValue();
}
}
Maven code:
<?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.midniteit</groupId>
<artifactId>TrOauth2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<exec.mainClass>TrOauth2</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.34.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.4c</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.11.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>TrOauth2</mainClass>
</configuration>
<executions>
<execution>
<id>execute-after-compile</id>
<goals><goal>java</goal></goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I get the following results from Netbeans IDE when I run it:
Hello World!
googleCredentials: ServiceAccountCredentials{clientId=xxx, clientEmail=firebase-adminsdk-xxx#lora-alarm.iam.gserviceaccount.com, privateKeyId=xxx, transportFactoryClassName=com.google.auth.oauth2.OAuth2Utils$DefaultHttpTransportFactory, tokenServerUri=https://oauth2.googleapis.com/token, scopes=[https://www.googleapis.com/auth/firebase.messaging], defaultScopes=[], serviceAccountUser=null, quotaProjectId=null, lifetime=3600, useJwtAccessWithScope=false, defaultRetriesEnabled=true}
java.lang.NullPointerException
at TrOauth2.getAccessToken (TrOauth2.java:43)
at TrOauth2.main (TrOauth2.java:29)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:835)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
When I run program through command line, I get this, without the googleCredentials line being printed:
C:\Users\administrator\Documents\NetBeansProjects\TrOauth2\target\classes>"c:\progra~1\java\jdk-12~1.1\bin\java" TrOauth2
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/auth/oauth2/GoogleCredentials
at TrOauth2.getAccessToken(TrOauth2.java:41)
at TrOauth2.main(TrOauth2.java:29)
Caused by: java.lang.ClassNotFoundException: com.google.auth.oauth2.GoogleCredentials
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
How is this happening, or what might I be doing wrong? Any ideas would be appreciated.

JavaFX play audio

Hey I'm new to javaFX and I'm trying to play an audio file.
There are already many people asking this quation on the internet, but it didn't help me .
First of all I want to initialize an audio file in the control.java file.
Later, I like to add audio FX to buttons and Animation.
I tried a local file, a URL, mp3 file, wav file, long audio files, 1 second audio file,
but non of these tests work.
some extra info:
java 14.0.1
javaFX 14.0.1
Linux
Eclipse
ERRORS:
java version: 14.0.1
javafx.version: 14.0.1
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NoClassDefFoundError: javafx/scene/media/Media
at playerAudio.MyControl.<init>(MyControl.java:14)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:124)
at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:346)
at java.base/java.lang.Class.newInstance(Class.java:604)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at playerAudio.MyMain.start(MyMain.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 31 more
Exception running application playerAudio.MyMain
I find it weird a error line, refuring to the MyMain.java:19
Main.javaParent root = FXMLLoader.load(getClass().getResource("MyLayout.fxml"));
not sure what this has to do with a audio player or audio file
And MyControl.java:14 is this one
Media media = new Media(new File(path).toURI().toString());
Main.java
package playerAudio;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
public class MyMain extends Application {
#Override
public void start(Stage stage) throws Exception {
System.out.println("java version: " +System.getProperty("java.version"));
System.out.println("javafx.version: " +System.getProperty("javafx.version"));
// Exception = sub class
Parent root = FXMLLoader.load(getClass().getResource("MyLayout.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Control.java
package playerAudio;
import java.io.File;
import javafx.fxml.FXML;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class MyControl {
//local
String path = "/home/lucas/eclipse-workspace/sandbox/src/playerAudio/open.wav";
Media media = new Media(new File(path).toURI().toString());
// url
//Media media = new Media("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3");
// get MediaPlayer
MediaPlayer mediaPlayer = new MediaPlayer(media);
#FXML public void initialize() {
System.out.println("initialize");
// play media file
mediaPlayer.play();
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="playerAudio.MyControl">
<children>
<Label text="Hello world FXML"/>
</children>
</AnchorPane>

Getting Internal 500 error while running my 1st servlet prog using netbeans [duplicate]

This question already has answers here:
#WebServlet fails in Netbeans 11.0 with java.lang.RuntimeException: com.example.NewServlet.<init>(NewServlet.java:1)
(2 answers)
Closed 2 years ago.
I am trying to create my 1st servlet program but while running my servlet I am getting internal server 500 error saying Error instantiating servlet class and
Exception:-
javax.servlet.ServletException: Error instantiating servlet class [com.servlets.MyFirstServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
Root cause:-
java.lang.RuntimeException:
com.servlets.MyFirstServlet.<init>(MyFirstServlet.java:1)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
is there any environment variable setup required? if so pls guide me on how to do that. Here is my directory.
enter image description here
and here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--servlet defn-->
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>com.servlets.MyFirstServlet</servlet-class>
</servlet>
<!--servlet mapping-->
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/web</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
and my servlet class:-
package com.servlets;
import java.io.IOException;
import javax.servlet.*;
public class MyFirstServlet implements Servlet {
ServletConfig conf;
#Override
public void init(ServletConfig conf) throws ServletException {
this.conf = conf;
System.out.println("Creating servlet object ...Inside init method");
}
#Override
public ServletConfig getServletConfig() {
return this.conf;
}
#Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
System.out.println("Inside Service Method...");
}
#Override
public String getServletInfo() {
return "This servlet is created by Ashish Raj...";
}
#Override
public void destroy() {
System.out.println("Inside Destroy method...going to destroy servlet");
}
}
Pls co-operate with me I m new to this... :)
Ultimately Netbeans doesn't know how to build this correctly. It's trying to run your Java file directly and it can't do that.
Can I recommend a simpler start? I'm not sure where you got that servlet code but it's highly likely not what you want. You need two files, placed in very specific directories.
First at the root of your development area for this project you'll need a file named pom.xml. It contains:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.servlet</groupId>
<artifactId>servlet-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Again, this is at the root of your project. Next, you need your servlet. This lives in src/main/java/com/example/servlet and has:
package com.example.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(name = "MyFirstServlet", urlPatterns = "/web")
public class MyFirstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("in doPost()");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("in doGet()");
}
}
Lastly, to help your IDE you should create the directory src/main/webapp/WEB-INF. There will be nothing in there but it tells Netbeans that this is a regular web-app.
So your entire directory will look like:
.
├── pom.xml
└── src
└── main
├── java
│   └── com
│   └── example
│   └── servlet
│   └── MyFirstServlet.java
└── webapp
└── WEB-INF
Import this into Netbeans. It is a Maven project if Netbeans asks.
When you run this it will be in a web-app. So, for example, when I ran it Tomcat put it into the servlet-example-1.0.0-SNAPSHOT webapp. That means that to access the services I had to go to http://localhost:8080/servlet-example-1.0.0-SNAPSHOT/web. The /web part is defined in your servlet in the urlPatterns parameter. Note that the servlet-example-1.0.0-SNAPSHOT part may be different depending on how Netbeans builds it for you.

EJB remote lookup namenotfoundexception

I try to remotely access an EJB in an (eclipse) EJB-Project, packaged as a jar, deployed on a WildFly 10.1.0 Final. The server side is no maven-project, so i added ejb-3.0.jar, javaee-api-7.0.jar, jboss-ejb3-ext-api-1.1.0.jar to the java build path.
My server code is:
package org.maometto.api;
public interface IMyBeanRemote {
void sayHello();
}
package org.maometto.businesslogic;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import org.maometto.api.IMyBeanLocal;
import org.maometto.api.IMyBeanRemote;
#Stateless
#Remote(IMyBeanRemote.class)
public class MyBeanImpl implements IMyBeanRemote {
#Override
public void sayHello() {
System.out.println("I am Server");
}
}
When I start the server from within eclipse the log shows:
21:06:19,015 INFO [org.jboss.as.ejb3.deployment] (MSC service thread
1-4) WFLYEJB0473: JNDI bindings for session bean named 'MyBeanImpl' in
deployment unit 'deployment "MyFirstEJB.jar"' are as follows:
java:global/MyFirstEJB/MyBeanImpl!org.maometto.api.IMyBeanRemote java:app/MyFirstEJB/MyBeanImpl!org.maometto.api.IMyBeanRemote
java:module/MyBeanImpl!org.maometto.api.IMyBeanRemote
java:jboss/exported/MyFirstEJB/MyBeanImpl!org.maometto.api.IMyBeanRemote
java:global/MyFirstEJB/MyBeanImpl java:app/MyFirstEJB/MyBeanImpl
java:module/MyBeanImpl
My Client - a converted maven project in an own project- code is as follows:
package com.maometto.client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.maometto.api.IMyBeanRemote;
public class EJbClient {
public static void main(String[] args) {
Properties jndiProps = new Properties();
//jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
jndiProps.put("jboss.naming.client.ejb.context", true);
try {
InitialContext initialContext = new InitialContext(jndiProps);
IMyBeanRemote iMyBeanRemote = (IMyBeanRemote) initialContext.lookup("java:global/MyFirstEJB/MyBeanImpl!org.maometto.api.IMyBeanRemote");
iMyBeanRemote.sayHello();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
To get the remote interface in the client, i added the ejb-project to the java build path of the client project.
The POM of the client is:
<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>MyEJBClient</groupId>
<artifactId>MyEJBClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>10.0.0.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-naming</artifactId>
<version>7.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-river</artifactId>
<version>1.4.0.Final</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jbossall-client</artifactId>
<version>4.2.2.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-ejb-client</artifactId>
<version>4.0.8.Final</version>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>11.0.0.Final</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I execute the client, i get following stacktrace in the log:
javax.naming.NameNotFoundException: global/MyFirstEJB/MyBeanImpl!org.maometto.api.IMyBeanRemote -- service jboss.naming.context.java.jboss.exported.global.MyFirstEJB."MyBeanImpl!org.maometto.api.IMyBeanRemote"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I tried it with the other jndiBindings as well.
I also tried it with the "ejb:" namespace and its convention ("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName). Still same error. I don't know what I'm missing or what I'm doing wrong.
Can anyone help me, please?

Two classes from one package FXMLLoader error

When I attempt to load an FXML file that is in the same package as the controller that is calling for the loading I get a ClassNotFound exception referring to the controller class of the FXML file that is being loaded. When I attempt to load an FXML file with the same name from a different package than the package containing the calling controller the file loads as expected.
Here is the code from the projectselectorcontroller class. The commented code is the line that causes the exception.
#Override
public void Response(KWMessage Message)
{
if(Message.getType() != -1)
{
switch(Message.getType())
{
case 6000:
Platform.runLater(new Runnable()
{
#Override
public void run()
{
//This file loads as expected
ExtendedNode node = mainController.load("/newproject/newproject.fxml");
//This file does not load
//ExtendedNode node = mainController.load("/projectselector/newproject.fxml");
NewProjectController controller = (NewProjectController)node.getController();
mainController.setCurrentNode(node);
}
});
}
}
}
Here is the load method. I have checked the URL and found that it is correct. Is it possible that two controller classes cannot reside in the same package?
public ExtendedNode load(String FXML)
{
ExtendedNode node;
URL location = getClass().getResource(FXML);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try
{
node = new ExtendedNode();
node.setNode((Parent)fxmlLoader.load(location.openStream()));
node.setController((iMainController)fxmlLoader.getController());
node.getController().setMainController(this);
}
catch (IOException ex){node = null;}
return node;
}
I am able to load FXML files located in the same package as the controller class file without difficulty. I am similarly able to load FXML files located in another package available relative to the one in which the controller class resides.
A common reason for a ClassNotFound Exception while loading FXML markup is not properly naming the controller class in the root level node of your markup file. You need a fully qualified Java object name in the fx:controller attribute, eg:
<AnchorPane id="AnchorPane" fx:id="ServicesEditor" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
styleClass="stage-pane" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.kkk.mm.censusassistant.GUIServicesEditorController">
I would first examine your markup files at both locations to assure that both had a fully qualified Java name for your controller class and that both identified the correct class.
If the controller class is properly identified in your project namespace and you are still getting a ClassNotFound exception, then I'll need more context in order to help you. Are you certain that it is the fxmlloader.load() method call that is generating this error? If not...
The logic you show for loading your markup seems convoluted. I'm not sure what the ExtendedNode objects are doing or why you're using them.
You're using:
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
:
:
node.setNode((Parent)fxmlLoader.load(location.openStream()));
Because you've already set the URL for the FXMLLoader with setLocation, you do not then need to call fxmlLoader with an inputStream. You can just simply use fxmlLoader.load();

Resources