I just wrote this code here:
package SpellcheckerClient;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = FXMLLoader.load(getClass().getResource("/controller/gui.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Spellchecker Client");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
And this is the corresponding controller.
package controller;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import spellchecker.remote.SpellcheckerRemoteAdapter;
public class Controller {
#FXML
TextField input;
#FXML
Button send;
#FXML
TextArea area;
#FXML
Button connect;
private SpellcheckerRemoteAdapter adapter;
#FXML
private void send() throws RemoteException{
String toCheck = input.getText();
this.area.appendText(toCheck + "\n");
this.area.appendText(checkRoutine(toCheck, this.adapter) + "\n\n");
this.input.clear();
}
public void initiateConnection() {
try {
Registry registry = LocateRegistry.getRegistry(1088);
this.adapter = (SpellcheckerRemoteAdapter) registry.lookup(SpellcheckerRemoteAdapter.NAME);
this.area.appendText("Verbindung erfolgreich aufgebaut!\n");
connect.setDisable(true);
} catch (Exception e) {
if(this.adapter == null) {
this.area.appendText("Server nicht gefunden!\n");
}
}
}
private static String checkRoutine(String input, SpellcheckerRemoteAdapter adapter) throws RemoteException {
if (input == null || input.isEmpty()) {
return "Bitte etwas eingeben!";
}
String[] words = input.split(" ");
boolean control = true;
String output = "";
for(String word : words) {
if(!adapter.check(word)) {
control = false;
output += word + ":\t" + adapter.getProposal(word) + "\n";
}
}
if(control) {
return "Alles Okay!\n";
}
return output;
}
}
If I run this code on my Laptop, where I wrote it, it runs perfectly fine in Eclipse and as runnable Jar. However, if I try to run the JAR on another computer i receive this error message:
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Unknown Source)
at javafx.graphics/com.sun.glass.ui.Application.lambda$run$1(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Unknown Source)
at javafx.graphics/com.sun.glass.ui.Application.lambda$run$1(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.graphics/com.sun.prism.d3d.D3DPipeline.getAdapterOrdinal(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.assignScreensAdapters(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$startup$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.Application.lambda$run$1(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
On my Laptop and my Computer are the same Versions of the JDK/JRE installed.
I don't really get what the error message is telling me.
Hello I have the same issue on my Eclipse environnement (Windows 10 OS),
adding in VM option -Djava.library.path="C:\WINDOWS\Sun\Java\bin" solved my problem
That means that javafx-graphics-[version]-win.jar call some native dll. And You have to found were those dll is stored.
I found the path, thanks to jvisualvm and showing the VM option for the case where it's the application run correctly.
Hoping that it will solve your problem.
Which JDk you have installed?? I have the same issue and i use JDK 8 instead of JDK 9. Which helped me.Sample Image of Exception
This is because jar which i created it was created on JDK 8 through a different system.
Where as when it was executing on another system which has JDK9. So it is version incompatible.
After keeping single JDK 8 and mapped it to system environment when i try to run my jar it worked for me.
Good Luck :)
Related
I'm trying to use a simple socket server to receive some commands(bukkit api).
With the thread, the plugin can receive commands and send it to the main server, so that i can control the server.
But when i tried to use a use a thread to solve the problem, an error happened:
CODE:
package tiance.auroracore;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import tiance.auroracore.metrics.Metrics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public final class AuroraCore extends JavaPlugin {
private int port;// 默认服务器端口
private String AcceptCommand;
public AuroraCore() {
this.port=9028;
this.AcceptCommand=new String();
}
// 创建指定端口的服务器
public AuroraCore(int port) {//构造方法
this.port = port;//将方法参数赋值给类参数
this.AcceptCommand=new String();
}
// 提供服务
public void service() {//创建service方法
ServerSocket server;
try {
server = new ServerSocket(port);//创建 ServerSocket类
}
catch (IOException e) {
System.out.println("Error! Cannot start the server! Is the port already used?");
return;
}
while (true) {
try {// 建立服务器连接
Socket socket = server.accept();// 等待客户连接
try {
DataInputStream in = new DataInputStream(socket
.getInputStream());// 读取客户端传过来信息的DataInputStream
DataOutputStream out = new DataOutputStream(socket
.getOutputStream());// 向客户端发送信息的DataOutputStream
while (true) {
String accept = in.readUTF();// 读取来自客户端的信息
AcceptCommand = accept;
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), AcceptCommand);
}
} finally {// 建立连接失败的话不会执行socket.close();
socket.close();//关闭连接
server.close();//关闭
}
} catch (IOException e) {//捕获异常
e.printStackTrace();
}
}
}
#Override
public void onEnable() {
new BukkitRunnable(){
public void run(){
new AuroraCore().service();//调用service方法
}
}.runTaskAsynchronously(this);
int pluginId = 13929;
Metrics metrics = new Metrics(this, pluginId);
saveDefaultConfig();
}
#Override
public void onDisable() {
}
}
ERROR
[12:45:28 WARN]: [AuroraCore] Plugin AuroraCore v1.0.0 generated an exception while executing task 2
java.lang.IllegalArgumentException: Plugin already initialized!
at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:218) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at tiance.auroracore.AuroraCore.<init>(AuroraCore.java:19) ~[?:?]
at tiance.auroracore.AuroraCore$1.run(AuroraCore.java:69) ~[?:?]
at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftTask.run(CraftTask.java:76) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:54) [craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_281]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_281]
Caused by: java.lang.IllegalStateException: Initial initialization
at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:221) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at tiance.auroracore.AuroraCore.<init>(AuroraCore.java:19) ~[?:?]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_281]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_281]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_281]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_281]
at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_281]
at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:79) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:143) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:393) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:301) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at org.bukkit.craftbukkit.v1_16_R3.CraftServer.loadPlugins(CraftServer.java:379) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:218) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:905) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:263) ~[craftbukkit-1.16.5.jar:3096a-Bukkit-af1a232]
... 1 more
I've edited the code again and again,but didn't found any solution. I've also seen some questions on the website, but none of them could solve my problem.I didn't found any repeat main class .I really don't know what to do! If someone can help, i will be very glad and thankful to him or her.
Replace new AuroraCore() with AuroraCore.this.
You have to replace new AuroraCore() with AuroraCore.this to initialize service() void
#Override
public void onEnable() {
new BukkitRunnable(){
public void run(){
AuroraCore.this.service();//调用service方法
}
}.runTaskAsynchronously(this);
int pluginId = 13929;
Metrics metrics = new Metrics(this, pluginId);
saveDefaultConfig();
}
This is my first question here, so I hope to be clear.
I am developing a simple JavaFX Application, the only thing it has to do is showing a web user interface.
Everything seems to be ok, I exported the Runnable JAR File from Eclipse and I tested it on Window 10 and 7, but when I put the jar on Window XP OS, I have the following:
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm
pl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsatisfiedLinkError: Invalid URL for class: rsrc:com/sun/g
lass/utils/NativeLibLoader.class
at com.sun.glass.utils.NativeLibLoader.loadLibraryFullPath(NativeLibLoad
er.java:162)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoad
er.java:94)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:
39)
at com.sun.webkit.WebPage.lambda$static$39(WebPage.java:130)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.webkit.WebPage.<clinit>(WebPage.java:129)
at javafx.scene.web.WebEngine.<init>(WebEngine.java:879)
at javafx.scene.web.WebEngine.<init>(WebEngine.java:866)
at javafx.scene.web.WebView.<init>(WebView.java:273)
at application.Main.start(Main.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162
(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Platfor
mImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.
java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformI
mpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.ja
va:191)
... 1 more
Here is my code:
package application;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
public class Main extends Application {
#Override
public void start(Stage stage)
{
stage.setMinWidth( 800 );
stage.setMinHeight( 600 );
stage.setMaximized( true );
Scene scene = new Scene(new Group());
WebView browser = new WebView();
WebEngine we = browser.getEngine();
//setting personalized context menu
browser.setContextMenuEnabled( false );
//createContextMenu( browser );
//preventing bug when maximmizing
stage.maximizedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed( ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1 )
{
System.out.println("maximized:" + t1.booleanValue());
}
});
//intercepting javascript alert
browser.getEngine().setOnAlert( new EventHandler<WebEvent<String>>()
{
#Override
public void handle(WebEvent<String> e)
{
Alert alert = new Alert( AlertType.WARNING );
alert.setTitle( "MyApp" );
alert.setHeaderText( null );
alert.setContentText( e.getData() );
alert.showAndWait();
System.out.println("JS alert() message: " + e.getData() );
}
});
//intercepting javascript confirm
browser.getEngine().setConfirmHandler( new Callback<String, Boolean>()
{
#Override
public Boolean call( String s )
{
Boolean ret;
ButtonType ok, cancel;
ok = new ButtonType( "Ok" );
cancel = new ButtonType( "Abort" );
Alert alert = new Alert( AlertType.CONFIRMATION );
alert.setTitle( "SameLAB" );
alert.setHeaderText( s );
alert.setContentText( null );
alert.getButtonTypes().setAll( ok, cancel );
alert.showAndWait();
if( alert.getResult() == ok )
ret = true;
else
ret = false;
return ret;
}
});
ScrollPane sp = new ScrollPane();
sp.setFitToWidth( true );
sp.setFitToHeight( true );
sp.setContent(browser);
//loading and showing content
we.load("https://www.google.it/");
scene.setRoot( sp );
stage.setScene( scene );
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Hope that someone cal help me, thank you!
I encountered this issue today after upgrading to the last JDK 121.
The same problem is with early release JDK 122.
As for now WebView and HTMLEditor works fine when using JDK 77 (probably some later versions too (as stated in the link below - 102), but i didn't check out for it).
Problem is with loading library NativeLibLoader.loadLibrary("jfxwebkit");
There were some vague suggestions that some new Microsoft Visual C++ Redistributables may help.
Edited.
It's already in bugreport: https://bugs.openjdk.java.net/browse/JDK-8170084 and, alas, closed with the Resolution: "Won't Fix".
So, one option is left - provide bundled JRE version 1.8.0_102 or less for Windows XP.
So, one option is left - provide bundled JRE version 1.8.0_102 or less for Windows XP. Another temporary dirty option: bundle your application with jfxwebkit.dll from 1.8.0_102 and add somewhere in your code before WebView instance created (for example, in start(Stage primaryStage) method)
if ("Windows XP".equals(System.getProperty("os.name"))){
System.load(ABS_PATH_TO_JFXWEBKIT_DLL + "\\jfxwebkit.dll");}
I am new to programming JavaFx and now in my initial stages i am building an addressbook in javaFX but problem presist that It throws an exception in constructor of Main Class.
When i use ObserverableList i dont get any kind of Exception, while when i try to use array to store Entity Objects i got Exception.
Can anybody give me a solution to that problem?
The main thing i want to do is to save ObserverableList or ArrayList object to a file.
package com.company;
import com.company.Controllers.DatabaseConnectJDBC;
import com.company.Controllers.MainWindowController;
import com.company.Controllers.secondWindowController;
import com.company.Model.EmployeeEntity;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.*;
import java.util.ArrayList;
public class Main extends Application {
Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage=primaryStage;
mainWindow();
}
public void mainWindow() {
try{
FXMLLoader loader=new FXMLLoader(this.getClass().getResource("Views/MainWindow.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
MainWindowController controller=loader.getController();
controller.setMain(this);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}catch (Exception e){
e.printStackTrace();
}
}
public void secondaryWindow() {
try{
FXMLLoader loader=new FXMLLoader(this.getClass().getResource("Views/secondaryWindow.fxml"));
AnchorPane pane=loader.load();
Scene scene=new Scene(pane);
Stage secondaryStage=new Stage();
secondWindowController controller=loader.getController();
controller.setMain(this,secondaryStage);
secondaryStage.setScene(scene);
secondaryStage.setResizable(false);
secondaryStage.show();
}catch (Exception e){
e.printStackTrace();
}
}
private ObservableList<EmployeeEntity> employeeObserveList= FXCollections.observableArrayList();
private ArrayList<EmployeeEntity> arrayList;
public ObservableList<EmployeeEntity> getEmployeeObserveList(){return employeeObserveList;}
public void setEmployeeObserveList(EmployeeEntity person){
getEmployeeObserveList().add(person);
}
public Main()
{
arrayList.add(new EmployeeEntity("3","126-B Millat Town","030123456","Abubaker","Usman"));
arrayList.add(new EmployeeEntity("4","127-B Millat Town","032123456","Asma","Rasool"));
arrayList.add(new EmployeeEntity("5","128-B Millat Town","035123456","Talha","Farooq"));
employeeObserveList = FXCollections.observableArrayList(arrayList);
employeeObserveList.addAll(arrayList);
}
Exception
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class com.company.Main
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at com.company.Main.<init>(Main.java:68)
... 13 more
change this line
private ArrayList<EmployeeEntity> arrayList;
To
private List<EmployeeEntity> arrayList = new ArrayList<EmployeeEntity>();
You never initialized ArrayList.
i am new in java... i am trying to read on modbus. PLC is slave device and it is configured well. my java file is unable to read modbus values.here is the code..given below. error is coming at master.init(); method. please help me in this case.
package com.mod4j;
import java.io.File;
import gnu.io.SerialPort;
import com.serotonin.io.serial.SerialParameters;
import com.serotonin.*;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.locator.BaseLocator;
import com.serotonin.modbus4j.locator.NumericLocator;
import com.serotonin.modbus4j.msg.ReadCoilsRequest;
import com.serotonin.modbus4j.msg.ReadCoilsResponse;
import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersResponse;
import com.serotonin.modbus4j.msg.WriteCoilRequest;
import com.serotonin.modbus4j.msg.WriteCoilResponse;
import com.serotonin.modbus4j.msg.WriteRegistersRequest;
import com.serotonin.modbus4j.msg.WriteRegistersResponse;
import gnu.io.*;
public class Modbus4JTest
{
public static void main(String[] args) throws Exception
{
try
{
SerialParameters params = new SerialParameters();
params.setCommPortId("COM1");
params.setBaudRate(9600);
params.setDataBits(8);
params.setStopBits(1);
params.setParity(0);
ModbusFactory modbusFactory = new ModbusFactory();
ModbusMaster master = modbusFactory.createRtuMaster(params);
master.setTimeout(100);
master.setRetries(3);
byte [] RIR,RHR,RDI,RCR;
int slaveId=1;
int startOffset=0;
int numberOfRegisters=10;
int numberOfBits=10;
try
{
master.init();
while (true)
{
ReadInputRegistersRequest reqRIR = new ReadInputRegistersRequest(slaveId, startOffset, numberOfRegisters);
System.out.println("ReadInputRegistersRequest reqRIR =" +reqRIR);
ReadInputRegistersResponse resRIR = (ReadInputRegistersResponse) master.send(reqRIR);
RIR = resRIR.getData();
System.out.println("InputRegisters :" + RIR);
ReadHoldingRegistersRequest reqRHR = new ReadHoldingRegistersRequest(slaveId, startOffset, numberOfRegisters);
ReadHoldingRegistersResponse resRHR = (ReadHoldingRegistersResponse) master.send(reqRHR);
RHR=resRHR.getData();
System.out.println("HoldingRegister :" + RHR);
ReadDiscreteInputsRequest reqRDI= new ReadDiscreteInputsRequest(slaveId, startOffset, numberOfBits);
ReadCoilsResponse resRDI = (ReadCoilsResponse) master.send(reqRDI);
RDI=resRDI.getData();
System.out.println("DiscreteInput :" + RDI);
ReadCoilsRequest reqRCR = new ReadCoilsRequest(slaveId, startOffset, numberOfBits);
ReadCoilsResponse resRCR = (ReadCoilsResponse) master.send(reqRCR);
RCR=resRCR.getData();
System.out.println("CoilResponce :" + RCR);
short[] sdata = null;
WriteRegistersRequest reqWR = new WriteRegistersRequest(slaveId, startOffset, sdata);
WriteRegistersResponse resWR = (WriteRegistersResponse) master.send(reqWR);
int writeOffset = 0;
boolean writeValue = true;
WriteCoilRequest reqWC = new WriteCoilRequest(slaveId, writeOffset, writeValue);
WriteCoilResponse resWC = (WriteCoilResponse) master.send(reqWC);
Thread.sleep(1000);
}//end while
}//end try
catch (Exception e)
{
e.printStackTrace();
}//end catch
finally
{
master.destroy();
}//end finally
}//end try
catch (Exception e)
{
e.printStackTrace();
}//end catch
}// end main
}//end class Modbus4JTest
this is java file i am running.
and here are the error i have got after compiling..
please suggest what went wrong and please correct me at ...
is there any step by step tutorial or any demo video please give me link at
ayyaz.nadaf#gmail.com
Exception in thread "main" java.lang.NoClassDefFoundError:
jssc/SerialPortException
at com.serotonin.io.serial.SerialUtils.openSerialPort(SerialUtils.java:94)
at com.serotonin.modbus4j.serial.SerialMaster.init(SerialMaster.java:58)
at com.serotonin.modbus4j.serial.rtu.RtuMaster.init(RtuMaster.java:45)
at com.mod4j.Modbus4JTest.main(Modbus4JTest.java:58)
Caused by: java.lang.ClassNotFoundException: jssc.SerialPortException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 4 more
It appears you're using Modbus4J. It is based on jSSC (Java Simple Serial Connector) for serial communication, so make sure that jSSC is found while compiling (you may need to download it separately, since you're getting a ClassNotFoundException related to a jSSC class).
I don't know about any tutorial but I may suggest you to take a look at the Modbus4J forum archive. Here's a simple Modbus RTU example.
This might be a stupid question from a newbie, so bear with me. Here is what I did:
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javax.sound.sampled.Clip;
public class Main {
public static void main(String[] args) {
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putHtml("<b>Some bold html</b>");
clipboard.setContent(content);
}
}
I got the following error:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/kaiyin/IdeaProjects/untitled8/out/production/untitled8:/Applications/IntelliJ IDEA 13 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.company.Main
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd -Dsun.java2d.xrender=true
Exception in thread "main" java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
at com.sun.glass.ui.Application.checkEventThread(Application.java:432)
at com.sun.glass.ui.ClipboardAssistance.<init>(ClipboardAssistance.java:40)
at com.sun.javafx.tk.quantum.QuantumToolkit.getSystemClipboard(QuantumToolkit.java:1127)
at javafx.scene.input.Clipboard.getSystemClipboardImpl(Clipboard.java:410)
at javafx.scene.input.Clipboard.getSystemClipboard(Clipboard.java:175)
at com.company.Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
Is it possible to access javafx cliboard without creating a gui at all?
I'll ignore the question of why you would ever want to do this...
As pointed out in the comments, you need to start the FX Application Thread in order to access the JavaFX clipboard. However, you don't actually need to show a Stage at any point.
The following compiles and runs:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.stage.Stage;
public class ClipboardTest extends Application {
#Override
public void start(Stage primaryStage) {
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putHtml("<b>Some bold html</b>");
clipboard.setContent(content);
Platform.exit();
}
public static void main(String[] args) {
launch(args);
}
}