Array Index Out of Bounds Exception - RMI DynamicClient - java

import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RMISecurityManager;
import java.rmi.server.RMIClassLoader;
import java.util.*;
import java.lang.reflect.Constructor;
public class DynamicClient {
public DynamicClient (String[] args) throws Exception {
Properties p = System.getProperties();
String url = p.getProperty("java.rmi.server.codebase");
Class ClasseClient = RMIClassLoader.loadClass(url,"ClientDistant");
Constructor [] C = ClasseClient.getConstructors();
C[0].newInstance( new Object[] {args} );
}
public static void main (String[] args) {
System.setSecurityManager( new RMISecurityManager() );
try {
DynamicClient cli = new DynamicClient(args);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
This code is supposed to dynamically charge the class ClientDistant from a web server and execute the treatment, however it displays an error message:
C:\Users\DELL\Desktop>java -Djava.security.policy=client.policy -Djava.rmi.serv
er.useCodebaseOnly=false -Djava.rmi.server.codebase=http://localhost:8080/RMI/
DynamicClient
java.lang.ArrayIndexOutOfBoundsException: 0
where RMI is a directory's in the java web application named RMI ( I m using GlassFish server).
Any solutions to load ClientDistant and execute its treatment?

Related

JNDI Reference+RMI, load remote .class failed

my java version is: 1.8.0_282
this is client:
import java.rmi.registry.*;
import javax.naming.*;
public class RegistryClient {
public static void main(String[] args) throws Exception {
System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase", "true");
Context registry = new InitialContext();
registry.lookup("rmi://127.0.0.1:1099/Demo");
System.out.println("done");
}
}
this is server:
import java.rmi.registry.*;
import javax.naming.*;
import com.sun.jndi.rmi.registry.ReferenceWrapper;
public class RegistryServer {
public static void main(String[] args) throws Exception {
Registry registry = LocateRegistry.createRegistry(1099);
Reference refObj = new Reference(
"xxx",
"RMIRegistryDemoRemote",
"http://127.0.0.1:8000/"
);
ReferenceWrapper hello = new ReferenceWrapper(refObj);
registry.bind("Demo", hello);
System.out.println("[!] server is ready");
}
}
this is the interface and implement of RMIRegistryDemo:
import java.rmi.*;
public interface RMIRegistryDemo extends Remote {
String sayHello(String name) throws Exception;
}
import java.rmi.server.*;
import java.rmi.*;
public class RMIRegistryDemoImpl extends UnicastRemoteObject implements RMIRegistryDemo {
public RMIRegistryDemoImpl() throws Exception {}
String id = "10";
#Override
public String sayHello(String name) {
System.out.println(id);
return "Hi, " + name;
}
}
this is the remote .class:
import java.io.IOException;
public class RMIRegistryDemoRemote {
public RMIRegistryDemoRemote() throws IOException {
final Process process = Runtime.getRuntime().exec("/System/Applications/Calculator.app/Contents/MacOS/Calculator");
}
}
after:
run RegistryServer
deployed a web server to send RMIRegistryDemoRemote.class
run RegistryClient
the client just prints "done", and no access log in my weblog:
# overflow in ~/Downloads/test [16:16:44]
» javac RegistryClient.java && java RegistryClient
done
# overflow in ~/Downloads/test/remote [16:20:05]
» python -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
what causes it?

ClassNotFoundException with RMI

i wrote a program in netbeans with RMI that client has error
error :
java.rmi.UnmarshalException: error unmarshalling return; nested
exception is: java.lang.ClassNotFoundException: rmiserver.Message
(no security manager: RMI class loader disabled) at
sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
but sever does not any error!
interfaace code:
package rmiclient;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Message extends Remote {
void sayHello(String name) throws RemoteException;
}
interface implementation is:
package rmiserver;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MessageImpl extends UnicastRemoteObject implements Message {
public MessageImpl() throws RemoteException {
}
#Override
public void sayHello(String name) throws RemoteException {
System.out.println("hello "+name);
}
}
server code is:
package rmiserver;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Main {
private void startServer(){
try {
// create on port 1099
Registry registry = LocateRegistry.createRegistry(1099);
// create a new service named myMessage
registry.rebind("myMessage", new MessageImpl());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("system is ready");
}
public static void main(String[] args) {
Main main = new Main();
main.startServer();
}
}
client code is:
package rmiclient;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Main {
private void doTest(){
try {
// fire to localhost port 1099
Registry myRegistry = LocateRegistry.getRegistry("127.0.0.1", 1099);
// search for myMessage service
Message impl = (Message) myRegistry.lookup("myMessage");
// call server's method
impl.sayHello("edwin");
System.out.println("Message Sent");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Main main = new Main();
main.doTest();
}
}
thanks :).
In your stacktrace:
java.lang.ClassNotFoundException: rmiserver.Message
but as per data you have provided, your Message interface is declated with package package rmiclient; and You haven't created any rmiserver.Message class.
Correct the package name.
I have two package. rmiclient and rmiserver. that in rmiclient are "message" and "main" . in rmiserver are "message" and "main" and "messageimpl"
That's your problem. This doesn't satisfy the contract. You can't just copy Message to another package and expect to have it be treated the same as the original. The remote stub implements the same remote interfaces that the remote object does. Not another interface with the same name in another package.
You have to deploy rmiserver.Message to the client. Just like the error message says, really.

how to create HttpsServer object same like HttpServer object in jersey

How to create HttpsServer Object & config webservice class using jersey as given below code to create HttpServer.
import java.net.URI;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import com.sun.net.httpserver.HttpServer;
public class ServerInit {
static final String BASE_URI = "https://localhost:9099/";
public static void main(String[] args) throws Exception {
HttpServer server = null;
ResourceConfig rc = new ResourceConfig(HelloWorld.class);
URI endpoint = new URI(BASE_URI);
server = JdkHttpServerFactory.createHttpServer(endpoint, rc);
System.out.println("console : Press Enter to stop the server. ");
System.in.read();
server.stop(0);
}
}

Oozie Java API Kerberos Authentication

I am trying to get an oozie job status using oozie java API. Currently it is failing with the message
Exception in thread "main" HTTP error code: 401 : Unauthorized
We are using a kerberos authentication in our cluster with a keytab file.
Please guide as how to proceed to implement the authentication.
My current program is:
import org.apache.oozie.client.OozieClient;
public class oozieCheck
{
public static void main(String[] args)
{
// get a OozieClient for local Oozie
OozieClient wc = new OozieClient(
"http://myserver:11000/oozie");
System.out.println(wc.getJobInfo(args[1]));
}
}
I figured a way to use kerberos in my java api.
First obtain kerberos tgt.
Then the below code works:
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.oozie.client.AuthOozieClient;
import org.apache.oozie.client.WorkflowJob.Status;
public class Wrapper
{
public static AuthOozieClient wc = null;
public static String OOZIE_SERVER_URL="http://localhost:11000/oozie";
public Wrapper ( String oozieUrlStr ) throws MalformedURLException
{
URL oozieUrl = new URL(oozieUrlStr);
// get a OozieClient for local Oozie
wc = new AuthOozieClient(oozieUrl.toString());
}
public static void main ( String [] args )
{
String lineCommon;
String jobId = args[0]; // The first argument is the oozie jobid
try
{
Wrapper client = new Wrapper(OOZIE_SERVER_URL);
Properties conf = wc.createConfiguration();
if(wc != null)
{
// get status of jobid from CLA
try
{
while (wc.getJobInfo(jobId).getStatus() == Status.RUNNING)
{
logger.info("Workflow job running ...");
logger.info("Workflow job ID:["+jobId+"]");
}
if(wc.getJobInfo(jobId).getStatus() == Status.SUCCEEDED)
{
// print the final status of the workflow job
logger.info("Workflow job completed ...");
logger.info(wc.getJobInfo(jobId));
}
else
{
// print the final status of the workflow job
logger.info("Workflow job Failed ...");
logger.info(wc.getJobInfo(jobId));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
System.exit(9999);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
You have to patch the oozie client if docs do not mention Kerberos.

Callback File System: eldos.cbfs.ECBFSError: Access is denied

I've requested trial license for Callback File System and tried to write simple application using java! So, I've written next few lines and run it and received exception eldos.cbfs.ECBFSError: Access is denied
Code
import eldos.cbfs.CallbackFileSystem;
import eldos.cbfs.ECBFSError;
import eldos.cbfs.boolRef;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* #author Sergii.Zagriichuk
*/
public class Test1 {
private static Logger logger = Logger.getLogger(Test1.class.getName());
public static void main(String[] args) {
CallbackFileSystem callbackFileSystem = new CallbackFileSystem();
callbackFileSystem.setRegistrationKey("My registration key ");
try {
callbackFileSystem.install("<path to cab>\\cbfs.cab", "Test", true, 131072, new boolRef(false));
} catch (ECBFSError ecbfsError) {
logger.log(Level.SEVERE, ecbfsError.getMessage(), ecbfsError);
}
}
}
What do I should to do for fix this problem?
Thanks

Categories