It seems this problem is pretty common in java, but what I've tried isn't working and I might need some guidance. I also tried running the same code on another machine and I didn't run into this issue.
My class Node.java uses NodeInterface.java but when running Node.java I get the error: java.lang.ClassNotFoundException: NodeInterface.
So far I've made sure that everything is compiled, and I've used the command java -cp /home/ryan/Desktop/P2P NodeInterface but this produced the error Error: Main method not found in class NodeInterface. Which confuses me because I didn't think interfaces are supposed to have main methods.
I'm not sure if this will be helpful, but the environment file located in etc has PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin". I'm running ubuntu 18.04.5 LTS and using vscode with the java extension pack. My file structure in vs code
Please let me know if I'm missing any information that could make this problem easier to solve.
Edit: here is a more simplified version of the code. However, I don't think the problem lies in the code and instead has something to do with the classpath.
Node.java:
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Node implements NodeInterface {
private String URL;
public Node(String URL) throws RemoteException {
try {
this.URL = URL;
}
catch(Exception e) {
System.out.println(String.format("Encountering issues while constructing the server class. Error %s%n", e.getMessage()));
}
}
public static void main(String[] args) {
try {
Node node = new Node(args[0]);
System.out.println(InetAddress.getLocalHost().getHostName());
System.setProperty("java.rmi.server.hostname", InetAddress.getLocalHost().getHostName());
NodeInterface serverStub = (NodeInterface) UnicastRemoteObject.exportObject(node, 0);
Naming.bind(args[0], serverStub);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
NodeInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface NodeInterface extends Remote {
}
Related
Please note I do not have any previous experience with Java. I am having issues with the following tutorial for Py4j: https://www.py4j.org/getting_started.html
I installed Py4j in an Anaconda environment. I am working in Ubuntu. I set my classpath to include the .jar file for Py4j. When I try to compile the sample code on the above web-page I received an error saying the Stack symbol didn't exist. I tried to add a line of code to import it, but that did not help either (see images).
Error and Directory Structure (image)
Source code:
Stack.java
package py4j.examples;
import java.util.LinkedList;
import java.util.List;
public class Stack {
private List<String> internalList = new LinkedList<String>();
public void push(String element) {
internalList.add(0, element);
}
public String pop() {
return internalList.remove(0);
}
public List<String> getInternalList() {
return internalList;
}
public void pushAll(List<String> elements) {
for (String element : elements) {
this.push(element);
}
}
}
StackEntryPoint.java
package py4j.examples;
import py4j.GatewayServer;
import py4j.examples.Stack; // <-- I added this line but it does not solve the issue
public class StackEntryPoint {
private Stack stack;
public StackEntryPoint() {
stack = new Stack();
stack.push("Initial Item");
}
public Stack getStack() {
return stack;
}
public static void main(String[] args) {
GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint());
gatewayServer.start();
System.out.println("Gateway Server Started");
}
}
I haven't used Java before, so I'm confused about how to link classes during compilation. I've tried looking extensively at Java documentation/resources online and questions related, but can't seem to solve this problem. Could someone point out what I'm doing incorrectly?
Thank you.
You are compiling class StackEntryPoint, but you have not compiled the Stack class yet. Do so first, otherwise it cannot use it to compile StackEntryPoint.
Normally your IDE would solve this for you, but you've got a special setup going here (with the Python integration) so I'm not sure how you'd integrate it.
Ideally, you'll build your Java library as a separate JAR using conventional Java tooling (e.g. Maven and IntelliJ IDEA), but it's not a bad exercise to learn to do it bare-bones first.
I followed the instructions from this video https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html, but my code is throwing java.lang.ClassNotFoundException: interfaces.MyNativeImpl on the look up line. The code I'm using is almost exactly that of the video.
package interfaces;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{
public String sayHi();
}
in the android native directory
package interfaces;
public class MyNativeImpl {
public String sayHi() {
return "hi";
}
public boolean isSupported() {
return true;
}
}
and in the java code:
MyNative my = (MyNative)NativeLookup.create(MyNative.class);
if(my != null && my.isSupported()){
System.out.println("Hello!");
}
Where did I go wrong now?
That is perfectly fine code and will work on the device.
You are seeing the exception in the simulator because the native/internal_tmp directory is missing from your runtime classpath but it shouldn't cause a problem other than no native interfaces on the desktop:
SQLUtils.java:
import org.openide.util.Lookup;
import java.util.ServiceLoader; // This doesn't work either
public class SQLUtils {
public static DBDriver getDriver(String prefix) {
for(DBDriver e : Lookup.getDefault().lookupAll(DBDriver.class)) {
System.out.println(e.getPrefix());
if(e.getPrefix().equalsIgnoreCase(prefix)) {
return e;
}
}
return null;
}
}
MySQLDriver.java:
public class MySQLDriver implements DBDriver {
#Override
public String getPrefix() {
return "mysql";
}
}
DBDriver.java:
import java.io.Serializable;
public interface DBDriver extends Serializable {
public String getPrefix();
}
Main.java:
public class Main {
public static void main(String[] args) {
DBDriver d = SQLUtils.getDriver("mysql");
}
}
This does nothing when running it, it cannot find any classes implementing.
What the program is trying to do is get the driver that is entered as a parameter for SQLUtils.getDriver(String prefix) (in Main.java).
For some reason I cannot get this to work.
I'm not familiar with OpenIDE Lookup mechanism, but I am familiar with the Java ServiceLoader mechanism.
You need to provide a file in the META-INF/services/ folder describing what classes implement specific interfaces. From the Java Docs describing the ServiceLoader class is this example:
If com.example.impl.StandardCodecs is an implementation of the
com.example.CodecSet service then its jar file also contains a file
named
META-INF/services/com.example.CodecSet
This file contains the single line:
com.example.impl.StandardCodecs # Standard codecs implementing com.example.CodecSet
What you are missing is a similar file that needs to be included on your classpath or within your JAR file.
You don't include you package names so I cannot provide a more direct example to help solve your problem.
I dropped the NetBeans API and switched to Reflections. I implemented Maven and ran it with IntelliJ. Works well for me.
Here is the file MyRemoteImplement.java:
import java.rmi.*;
import java.rmi.server.*; //for UnicastRemoteObject
public class MyRemoteImplement extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Remote server says hello";
}
public MyRemoteImplement() throws RemoteException
{
;
}
public static void main(String[] args)
{
try
{
MyRemote server = new MyRemoteImplement();
Naming.rebind("Remote Hello Server", server);
}
catch(Exception rex)
{
System.out.println("Error when registering server.");
}
}
}
I compiled this code to obtain MyRemoteImplement.class
I then navigated to the directory containing the file MyRemoteImplement.class and ran the following command in command line(Windows XP):
rmic MyRemoteImplement
According to the text book I am following, both the stub code and the skeleton code must be generated in the same directory as a consequence of running the above command.
But, I only the stub file MyRemoteImplement_stub.class is generated, not the skeleton code.
Why is the skeleton code not generated?
How to rectify it?
The text book you are folliowing is 16 years out of date. Skeletons haven't been generated automatically since 1998. You don't need one.
I am trying to compile this:
public class DNSLookUp {
public static void main(String[] args) {
InetAddress hostAddress;
try {
hostAddress = InetAddress.getByName(args[0]);
System.out.println (hostAddress.getHostAddress());
}
catch (UnknownHostException uhe) {
System.err.println("Unknown host: " + args[0]);
}
}
}
I used javac dns.java, but I am getting a mess of errors:
dns.java:1: error: The public type DNSLookUp must be defined in its own file
public class DNSLookUp {
^^^^^^^^^
dns.java:3: error: InetAddress cannot be resolved to a type
InetAddress hostAddress;
^^^^^^^^^^^
dns.java:6: error: InetAddress cannot be resolved
hostAddress = InetAddress.getByName(args[0]);
^^^^^^^^^^^
dns.java:9: error: UnknownHostException cannot be resolved to a type
catch (UnknownHostException uhe) {
^^^^^^^^^^^^^^^^^^^^
4 problems (4 errors)
I have never compiled/done Java before. I only need this to test my other programs results. Any ideas? I am compiling on a Linux machine.
The file needs to be called DNSLookUp.java and you need to put:
import java.net.InetAddress;
import java.net.UnknownHostException;
At the top of the file
The answers given here are all good, but given the nature of these errors and in the spirit of 'teach a man to fish, etc, etc':
Install IDE of choice (Netbeans is an easy one to start with)
Setup your code as a new project
Click the lightbulb on the line where the error occurs
Select the fix you'd like
Marvel at the power of the tools you have available
Rename the file as DNSLookUp.java and import appropriate classes.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DNSLookUp {
public static void main(String[] args) {
InetAddress hostAddress;
try {
hostAddress = InetAddress.getByName(args[0]);
System.out.println(hostAddress.getHostAddress());
} catch (UnknownHostException uhe) {
System.err.println("Unknown host: " + args[0]);
}
}
}
You need to import the classes you're using. e.g.:
import java.net.*;
To import all classes from the java.net package.
You also can't have a public class DNSLookUp in a file named dns.java. Looks like it's time for a Java tutorial...
Coming from "The public type <<classname>> must be defined in its own file" error in Eclipse which was marked as duplicate.
I'm thus answering this "duplicate" here:
On Eclipse, you can prepare all your public classes in one file, then right-clic -> Refactor -> Move Type to New File.
That's not exactly the right workaround (this won't let you have multiple public classes in one file at compile time), but that will let you move them around in their proper files when you'll be ready.
Coming from C#, that's an annoying enforced limitation for do-forgettable (tutorial) classes, nonetheless a good habit to have.