Basic RMI using netbeans - java

I am staring to learn about RMI in java and I am using Netbeans 7.0.1. I created a basic interface
package helloclass;
import java.rmi.*;
public interface HelloInterface extends Remote
{
public String hello() throws RemoteException;
}
This is the class that implements the interface
package helloclass;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloClass extends UnicastRemoteObject implements HelloInterface
{
private String message;
public HelloClass() throws RemoteException{}//constructor throwing RemoteException
public String hello() throws RemoteException //method throwing RemoteException
{
return "Saying hello";
}
public static void main(String[] args)
{
}
}
In my understanding I now need to build these two classes and then run the rmic command in the command prompt.
How do I run this rmic command in the command prompt using netbeans?.
I have been trying by going to Project Properties and then typing rmic in the VM options at the Run after specifying the directory where HelloClass.class is located but it can't seem to find the class.

As of Java 5 there is no need to use the rmic command for generating stubs, they will be generated dynamically for you, check this
However, if you still want to support clients running on earlier version, run the command from the base directory of the compiled classes (in your case the directory that contains the "helloclass" directory) and use the class qualified name, e.g.: rmic helloclass.HelloClass

Related

Java Quarkus running a command line runner without main method

Hello I'm trying to follow the example here https://quarkus.io/blog/introducing-command-mode/
So I wrote a simple class with Java 11
#QuarkusMain
public class CommandRunner implements QuarkusApplication {
#Override
public int run(String... args) throws Exception {
System.out.println("hello command line runner");
return 0;
}
}
I build this with mvn clean package to get the .jar file
i then run java myjar.jar and i get the error
Error: Could not find or load main class
I thought Quarkus would generate a main method if I didn't provide one?
You have to run it with:
java -jar target/quarkus-app/quarkus-run.jar

How to launch program in psvm?

How to launch a program in psvm with one command?
How does the application know which class to launch first?
I know that psvm should only have starting command and nothing more.
Could you explain this to me?
I mean how to create proper public static void main(String[] args) in a simple program on Maven. Should I create a class i.e. Starter with method run (with sequence actions) and in psvm write new Starter().run()?
psvm stands for public static void main as shown below:
public static void main(String[] args) {
// Your code here
}
psvm is not a standard Java terminology. You can call it as a Java slang. It is the entry point in your standalone Java application i.e. when you run an executable jar, it will execute the class having psvm. There are so much of content about it on the internet e.g. https://dzone.com/articles/executable-java-applications
The main() is the first entry point of Java application. Java Virtual Machine is told to run an application by specifying its class using the application launcher & it will look for the main() with exact syntax of public static void main(String[]).
Considering your comments you want to do something like this :
public class Starter{
public static void main(String args) {
new Starter().run();
}
public void run() {
//your logic
}
}
once you write this,
you have multiple options to run this I am mentioning a few
1) by building jar and then executing that jar using java -jar command
2) or by executing maven command once you have compiled your program using mvn compile, mvn exec:java -Dexec.mainClass="complete name of your main class i.e including package name."
a few links
http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Maven_SE/Maven.html
hope this might help

Eclipse wants non-existent methods to be implemented

I have an application that I've been trying to get working with Java 8's RMI. I'm using eclipse Neon.3 on Windows 10 and it is complaining that I am not implementing methods of an interface that don't exist. In trying to narrow down a separate issue, I've commented out one of the abstract methods of the interface.
After commenting out the one abstract method, the interface is then exported to a jar file; the jar file is then added to the build path of the server application that will implement the interface.
The interface:
package accessService;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;
public interface ApplicationAccessService extends Remote{
//public void connectClient(Long[] clientId) throws RemoteException;
public HashMap<String, Long> getClientConnections() throws RemoteException;
public HashMap<String, Integer> getServerConnections() throws RemoteException;
}
The implementing class:
package iap.util;
import java.rmi.RemoteException;
import java.util.HashMap;
import accessService.ApplicationAccessService;
public class RemoteIAP implements ApplicationAccessService{
private final static HashMap<String, Integer> SERVERS = new HashMap<>();
private final static HashMap<String, Long> CLIENTS = new HashMap<>();
public RemoteIAP(){ }
//methods used by RMI interface--------------------------------------
#Override
public HashMap<String, Integer> getServerConnections() throws RemoteException {
return SERVERS;
}
#Override
public HashMap<String, Long> getClientConnections() throws RemoteException {
return CLIENTS;
}
//end RMI methods-------------------------------------------------------
}
The error:
The type RemoteIAP must implement the inherited abstract method ApplicationAccessService.connectClient(Long[])
It seems to me, that eclipse is somehow maintaining artifacts from before commenting out the method in question. Restarting eclipse and rebooting my pc has done nothing to change this behavior. I don't know if this is a problem with java, the manner I'm adding the interface to the build path using the context menu's in eclipse, or something else?
Sometimes, ide can read the old compiled .class file and somehow it does not replace with the new one after the build operation.
Change the class name and build again.
I think it's possible that taking out the line and then exporting may have failed. Understand that in the .jar, it is the code in the compiled .class file that matters, not in the .java file. So if you hadn't recompiled before exporting, you could have an old version in the .jar

Adding -classpath from command line

So I have two java files, Print.java and StaticImport.java, in src/com/test.
StaticImport.java:
package com.test;
import static com.test.*;
class StaticImport {
public static void main(String[] args) {
System.out.println("Hello world");
Print.print("This is cool");
}
}
Print.java:
package com.test;
public class Print {
public static void Print(String command) {
System.out.println(command);
}
}
So basically there is the StaticImport class that uses Print class.
How can I compile the StaticImport with javac in command line?
I have tried for example: javac -cp /home/pathToProj/ StaticImport.java, but with no success.
In java, the classpath contains class files, not java code.
First, you need to compile Print.java, since you need it to be on your classpath. Then you need to set the classpath for the compilation of StaticImport to be the directory containing the "com" directory above Print.class.
You can also compile both files at the same time, using a single call to javac.
However, the best thing to do is to use either maven or gradle to build it for you. They look after your classpath, and do a million other things besides.

java command line errors

I'm new to java and I'm trying to compile and run a web service example from a book.
The example uses 3 files.
I can create an Eclipse Project and Run it. It works fine this way.
From the command line I tried
javac TimeServer.java TimeServerImpl.java TimeServerPublisher.java
And got no errors
This program does not run on the command line returns error:
"Could not find the main class"
java TimeServerPublisher
running using the -classpath option returns the same result.
Set classpath does not help either. ie
java -classpath . TimeServerPublisher
fails as well
Most of the online docs specify I need a classpath. I tried everything they suggested.
Please Help. Thanks in advance
Source:
TimeServer.java
package ch01.ts;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer
{
#WebMethod String getTimeAsString();
#WebMethod long getTimeAsElapsed();
}
TimeServerImpl.java
package ch01.ts;
import java.util.Date;
import javax.jws.WebService;
#WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer
{
#Override
public String getTimeAsString()
{
return new Date().toString();
}
#Override
public long getTimeAsElapsed()
{
return new Date().getTime();
}
TimeServerPublisher.java
package ch01.ts;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher
{
public static void main(String[ ] args)
{
Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
}
}
Your class is not named TimeServerPublisher; it's named ch01.ts.TimeServerPublisher. Even if you manage to get the JVM to find your class file, it will reject it with a wrong-name error, as you must invoke the class with its full name.
Put all the class files into a directory ch01/ts if they're not there already, and from ch01's parent directory, type
java -cp . ch01.ts.TimeServerPublisher
I guarantee that done correctly this will work.
get rid of the package statements until you know how they work. to have that package, your sources and binaries should be under ./ch01/ts/ and you would compile and invoke as:
javac ch01/ts/*.java
java ch01.ts.TimeServerPublisher
Move all your class files to folder ch01/ts.
and then execute command
java ch01.ts.TimeServerPublisher
There you go. If you say javac -d ch01/ts *.java during compilation, it will be solved.

Categories

Resources