I have java.net.MalformedURLException - java

package RMI_Package;
import java.rmi.server.*;
import java.rmi.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello(){
return "Server says,'Hey'";
}
public MyRemoteImpl() throws RemoteException{}
public static void main(String [] args){
try{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello",service);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
This code is from Head First Java Book when i run it, it throws the java.net.MalformedURLException.

As specified by the Naming documentation, the first parameter of bind should be a valid URL.
As an example (taken from here):
Naming.bind("rmi://localhost:8800/YourObject", service);

Related

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

Error: Main method not found in class jsone.testing, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
package jsone;
import java.io.File;
import java.io.IOException;
import org.apache.xpath.operations.String;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class testing {
#Test
public static void main(String args[]) {
ObjectMapper mapper = new ObjectMapper();
try {
File jsonInputFile = new File("D:\\workspace\\jsone\\car.json");
car emp = mapper.readValue(jsonInputFile, car.class);
System.out.println(emp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package jsone;
public class car{
private String colour;
public String getcolour() {
return colour;
}
public void setcolour(String colour) {
this.colour = colour;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n----- Employee Information-----\n");
sb.append("Colour: " + getcolour() + "\n");
sb.append("*****************************");
return sb.toString();
}
}
In your example String class comes from org.apache.xpath.operations.String package which is wrong. You should use java.lang.String class instead. Classes from java.lang package are visible in class by default so you have to delete below line:
import org.apache.xpath.operations.String;
and it should start work. Also, your main method should not be annotated with #Test annotation.
Also, please take a look on this question:
Error: Main method not found in class MyClass, please define the main method as…

com.sun.proxy.$Proxy0 cannot be cast to

I have the following RMI Connection code which returns a com.sun.proxy.$Proxy0 cannot be cast to Client.AdditionInterface error. I created two separate packages Client & Server and putted the Interface in both of them. Here is my complete code:
package Serveur;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
public class AdditionServer {
public static void main (String[] argv) {
try {
LocateRegistry.createRegistry(1099);
Addition Hello = new Addition();
Naming.rebind("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC", Hello);
System.out.println("Addition Server is ready.");
}
catch (Exception e) {
System.out.println("Addition Server failed: " + e);
}
}
}
//////////////////////////////////////
package Serveur;
import java.rmi.*;
import java.rmi.server.*;
public class Addition extends UnicastRemoteObject implements AdditionInterface {
public Addition () throws RemoteException {
super();
}
#Override
public int add(int a, int b) throws RemoteException {
int result=a+b;
return result;
}
#Override
public String aff(int a, int b) throws RemoteException {
return String.valueOf(add(a, b));
}
}
////////////////////////////
package Client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface AdditionInterface extends Remote {
public int add(int a,int b) throws RemoteException;
public String aff(int a, int b) throws RemoteException;
}
////////////////////////////
package Client;
import java.rmi.*;
public class AdditionClient {
public static void main (String[] args) throws Exception {
AdditionInterface add = (AdditionInterface) Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC");
System.out.println("Result is :"+add.add(9, 10));
System.out.println(add.aff(26, 45));
}
}
Any help please? Thank you.
As in both a Client.AdditionInterface for the client and a Server.AdditionInterface for the server? Effectively the error is saying Server.AdditionInterface cannot be cast to Client.AdditionInterface.
With RMI you must have the same interface class on both client and server, but then clearly the server implementation class (AdditionServer) just on the server. The interface could be in a shared package (e.g. common.AdditionInterface ).

Loading Class from Jar File : java.lang.InstantiationException

I got this code to instantiate a Life class from jar file:
import com.life.Life;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class Main {
public static void main(String[] args) {
try{
File file = new File("Life.jar");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("com.life.Life");
Life life = (Life) cls.newInstance();
System.out.println("Message: "+life.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}
Here's the content of the Life.jar
public class Life {
public String getMessage(){
return "Life is Beautiful!";
}
}
Here's my interface name Life
package com.life;
public interface Life {
public String getMessage();
}
The code above will throw an error:
java.lang.InstantiationException: com.life.Life
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.Main.main(Main.java:20)
BUILD SUCCESSFUL (total time: 0 seconds)
What's wrong with the code? How to resolve this?
This happened because your interface is also named Life ( java tried to instantiate an interface). Change public interface Life to public interface LifeInterface and then have your class Life implement that like :
public class Life implements LifeInterface
{
#Override
public String getMessage()
{
return "Life is Beautiful!";
}
}

Error java.lang.NoSuchMethodError In java

I have problems with these methods.
The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: pkgModelo.AnalizadorLexico: method <init>()V not found
The classes are:
Class frmAnalizador:
package pkgVista;
import pkgModelo.AnalizadorLexico;
public class frmAnalizador extends javax.swing.JFrame {
AnalizadorLexico alexico;
String linea;
JFileChooser abrirArchivo;
public frmAnalizador() {
initComponents();
alexico = new pkgModelo.AnalizadorLexico();
}
}
In object alexico show the exception.
Class AnalizadorLexico:
package pkgModelo;
import java.io.FileInputStream;
public class AnalizadorLexico implements AnalizadorLexicoConstants {
public AnalizadorLexico() {
}
public static void principal(FileInputStream file) throws ParseException {
try {
AnalizadorLexico analizador = new AnalizadorLexico(file);
analizador.Algoritmo();
System.out.println("El analizador l\u00e9xico ha compilado correctamente");
}
catch(ParseException e) {
System.out.println("Hay errores: " + e.getMessage());
}
}
}
Here in this line AnalizadorLexico analizador = new AnalizadorLexico(file); you passed file object as a parameter where as your class AnalizadorLexico has not any kind of parameterized constructor so you have to make one more constructor which has a parameter of FileInputStream.
public AnalizadorLexico(FileInputStream file){
//Your Code
}

Call java method with dbus

How is it possible to export a java method or object using dbus?
I am writing this because the official documentation is very poor and it took me hours to figure out how to do it.
Ideally the DBus interface should go in a java package
DBus.java
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
#DBusInterfaceName("org.printer")
public interface DBus extends DBusInterface {
//Methods to export
public void Print(String message);
}
Main.java
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
try {
DBusConnection conn = DBusConnection.getConnection(DBusConnection.SESSION);
//Creates a bus name, it must contain some dots.
conn.requestBusName("org.printer");
//Exports the printer object
conn.exportObject("/org/printer/MessagePrinter", p);
} catch (DBusException DBe) {
DBe.printStackTrace();
conn.disconnect();
return;
}
}
}
//Printer object, implements the dbus interface and gets
//called when the methods are invoked.
class Printer implements DBus {
public boolean isRemote() {
return false;
}
public void Print(String message) {
System.out.println(message);
}
}
You can try this out with qdbus from the shell, running:
qdbus org.printer /org/printer/MessagePrinter org.printer.Print test

Categories