Java:无法找到或加载主类ubuntu

huangapple 未分类评论42阅读模式
英文:

Java: Could not find or load main class ubuntu

问题

这是我的文件布局
MUD-master/MUD/src/mud/MUDServer.java
注意,mud 包含了所有的 Java 类,但其他的类与这个问题无关。
实质上,我正试图在一个端口上初始化这个 MUDServer。
我写了一个理论上应该能工作的 bash 脚本,但我遇到了一些问题。这是 bash 脚本:

#!/bin/bash
echo "This is a test"
cd "MUD/src/mud"
echo "cd 'MUD/src/mud'"
#javac -cp /MUD/scr/mud/*.java
echo "javac *.java"
javac *.java

gnome-terminal -- rmiregistry 50010
gnome-terminal -- java mud.MUDServer 50010 50011
gnome-terminal -- java mud.clientMain localhost 50010 50012
gnome-terminal -- java mud.clientMain localhost 50010 50013
gnome-terminal -- java mud.clientMain localhost 50010 50014

程序可以运行到rmiregistry 50010这一行,然后停止。
我尝试手动初始化服务器,使用命令java mud.MUDServer 50010 50011,但我收到了这个错误信息:

Error: Could not find or load main class mud.MUDServer
Caused by: java.lang.ClassNotFoundException: mud.MUDServer

经过一些研究,我尝试了这个:

java -cp mud.MUDServer 50010 50011

这导致了这个错误:

Error: Could not find or load main class 50010
Caused by: java.lang.ClassNotFoundException: 50010

这是 MUDServer.java 的一部分代码:

package mud;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

/**
 *
 * This is the main class for initialisation of the MUD Server
 * It deals with the start up of the server, and also
 * covers any security issues that may arise from connecting 
 *
 */

public class MUDServer {

	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) 
	{
		if(args.length < 2)
		{
			System.err.println("Usage:\n Java MUDServer <registryport> <serverport>");
			return;
		}
		try 
		{
			String host = (InetAddress.getLocalHost()).getCanonicalHostName();
			int regPort = Integer.parseInt(args[0]);
			int serverPort = Integer.parseInt(args[1]);
			
			// Security
			System.setProperty("java.security.policy", "mud.policy");
			System.setSecurityManager(new RMISecurityManager());
			
			// Generate remote objects
			ServerImplementation mudServer = new ServerImplementation();
			ServerInterface mudInter = (ServerInterface)UnicastRemoteObject.exportObject(mudServer, serverPort);
			
			String regURL = "rmi://" + host + ":" + regPort + "/MUDServer";
			System.out.println("Registering" + regURL);
			Naming.rebind(regURL, mudInter);
		}
		catch(java.net.UnknownHostException e)
		{
			System.err.println(e);
		}
		catch(java.io.IOException e)
		{
			System.err.println(e);
		}
		

	}

}

希望能提供帮助,我已经花了一段时间尝试解决这个问题。

英文:

Here is my file layout
MUD-master/MUD/src/mud/MUDServer.java
Note that mud contains all my java classes, but the other ones aren't relevant to this question.
Essentially I'm trying to to initilise this MUDServer on a port.
I wrote a bash script that, in theory, should work but I'm having some issues. This is the bash script:

#!/bin/bash
echo &quot;This is a test&quot;
cd &quot;MUD/src/mud&quot;
echo &quot;cd &#39;MUD/src/mud&#39;&quot;
#javac -cp /MUD/scr/mud/*.java
echo &quot;javac *.java&quot;
javac *.java

gnome-terminal -- rmiregistry 50010
gnome-terminal -- java mud.MUDServer 50010 50011
gnome-terminal -- java mud.clientMain localhost 50010 50012
gnome-terminal -- java mud.clientMain localhost 50010 50013
gnome-terminal -- java mud.clientMain localhost 50010 50014

The program working up to the rmiregistry 50010 line, and then stops.
I try to manually initilise the server, but using the command
java mud.MUDServer 50010 50011, but I get this error message

Error: Could not find or load main class mud.MUDServer
Caused by: java.lang.ClassNotFoundException: mud.MUDServer

After some research I tried this:

java -cp mud.MUDServer 50010 50011

which got me this error:

Error: Could not find or load main class 50010
Caused by: java.lang.ClassNotFoundException: 50010

This is a snippet the MUDServer.java:

package mud;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

/**
 *
 * This is the main class for initialisation of the MUD Server
 * It deals with the start up of the server, and also
 * covers any security issues that may arise from connecting 
 *
 */

public class MUDServer {

	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) 
	{
		if(args.length &lt; 2)
		{
			System.err.println(&quot;Usage:\n Java MUDServer &lt;registryport&gt; &lt;serverport&gt;&quot;);
			return;
		}
		try 
		{
			String host = (InetAddress.getLocalHost()).getCanonicalHostName();
			int regPort = Integer.parseInt(args[0]);
			int serverPort = Integer.parseInt(args[1]);
			
			// Security
			System.setProperty(&quot;java.security.policy&quot;, &quot;mud.policy&quot;);
			System.setSecurityManager(new RMISecurityManager());
			
			// Generate remote objects
			ServerImplementation mudServer = new ServerImplementation();
			ServerInterface mudInter = (ServerInterface)UnicastRemoteObject.exportObject(mudServer, serverPort);
			
			String regURL = &quot;rmi://&quot; + host + &quot;:&quot; + regPort + &quot;/MUDServer&quot;;
			System.out.println(&quot;Registering&quot; + regURL);
			Naming.rebind(regURL, mudInter);
		}
		catch(java.net.UnknownHostException e)
		{
			System.err.println(e);
		}
		catch(java.io.IOException e)
		{
			System.err.println(e);
		}
		

	}

}



Any help would be appreciated, I've spend a while trying to solve this issue.

huangapple
  • 本文由 发表于 2020年4月4日 02:55:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61018694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定