rmic error class not found - java

I'm reading head first java and I'm about to try the ready baked codes that shows how to use RMI.
These are the classes:
The remote interface
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
The remote implementation
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello() {
return "Server Says,Hello";
}
public MyRemoteImpl() throws RemoteException { }
public static void main(String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Then I placed the .java and the .class file in c:\RMI. When running it, it says MyRemoteImpl class not found even though I'm running from the same directory. How can i fix this? Thanks.
EDIT: The error appear when I try to run this command
rmic MyRemoteImpl

The comments of BuddingProgrammer worked for my as well.
For instance if your classes are in folder C:\users\renato\workspace\ADI\src\semana5 you should go to one level above:
C:\users\renato\workspace\ADI\src and you should compile like this: javac semana5\Calculadora.java
To run RMI you should type the following command in C:\users\renato\workspace\ADI\src:
rmic semana5.CalculadoraImp

Create a folder name it HelloServer
Add a this package package HelloServer; on the top of each class, MyRemote and MyRemoteImpl.
Open cmd and write javac HelloServer/MyRemote.java and javac HelloServer/MyRemoteImpl.java from the directory that contain the HelloServer folder.
Write rmic HelloServer.MyRemoteImpl from the directory that contain the HelloServer folder.
You should have now a MyRemoteImpl_stub.class and you can have a nice day :)
PS: It is important that the package name is different than RMI or any object used inside the class. Otherwise you will have object collision.

Since I had the same problem and none of these answers helped me, I'll add what worked for me. I was running rmic MyRemoteImpl in the directory that contained MyRemoteImpl.java and got the same error. When I ran rmic MyRemoteImpl in the directory that contained MyRemoteImpl.class, it worked and created MyRemoteImpl_Stub.class. I did not need to set CLASSPATH.

if the class is located in C:/../src/Assign/implement.class then
change your directory in cmd by entering:- cd C:/....../src
run the command:- rmic Assign.implement.class
It worked for me

If you're not running this command from the directory containing the .class file, you should be, assuming there is no package statement as per what you posted. If there's a package statement you should be running this command from the directory that contains the highest-level package.
Otherwise there's something wrong with your CLASSPATH environment variable. It should at least contain ".", probably some other stuff as well. As a workaround you can adopt Isaac's suggestion but then you'll only get the same problem when you come to compile and execute.

I have the same code, but in a Maven project. First step is to compile all the code with mvn compile. Note that all compiled classes (ex.MyRemoteImpl in this case) are stored in the target folder in your maven project.
The next step is to cd to \target\classes folder.
Finally, run the rmic command with the fully qualified name of the target class, i.e with the packages of the class - rmic my_package1.my_sub_package.MyRemoteImpl.

I know I'm about 8 years too late, but just for anyone else in the future who might come across this same problem, the following worked for me:
In the folder that contains your files, first start off with javac *.java and then proceed with the rmic class_name step. This should generate the stub.

There is a path variable called CLASSPATH where the JVM will look for class files to excute when you try to run a class file.
CLASSPATH describes the location where all the required files are available which are used in the application.
Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the required files.
If the CLASSPATH is not set, Java Compiler will not be able to find the required files and hence will throw error.
If you don't know where your current path is, then type echo "%CLASSPATH%" in the command prompt.
Alternatively, you can also add/remove/edit the CLASSPATH variable in the Environmental Variables.
Make sure that you have added your Java to your path.
To add the current path where your .class files exists there are three ways.
Using Environmental variables - Reference
Using Command Prompt - set PATH = .
Specify the class path using -classpath argument while executing.
Semi-colon (;) is used as a separator and dot (.) is the default value of CLASSPATH while specifying the class path using -classpath argument.
rmic -classpath . <Class Name>
rmic -classpath "C:\Example\Demo" <Class Name>
Here . stands for the current directory and you can specify any path there.

Try adding:
-classpath .
By default, rmic's classpath is derived from the CLASSPATH environment variable (thanks EJP for the correction).

Related

Error; Could not find or load main class (Java using Windows CMD)

I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java. tests is the package I created in Eclipse and src is a folder that Eclipse made under Introduction (which is the project name).
In my environment variables, I have the following relevant variable:
JAVA_HOME C:\Program Files (x86)\Java\jdk1.7.0_40\bin
Under system variables I have the following:
CLASSPATH %JAVA_HOME%
I go to my cmd and cd into the tests directory (cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests). Then I compile using javac Test.java. This seems to work as I then have a Test.class file under the same directory. Now I want to run the file, I type java Test and I get the error, "could not find or load main class". I've tried a variety of things including appending .class and .java to the end but I keep getting the error. I looked at some answers and docs and I managed to get it to work if I cd into:
cd C:\Users\MYNAME\Documents\Java\Introduction\src (i.e, get out of the package)
and then run:
java -cp . tests.Test
So that seems to temporarily set the class path to the current directory, and run Test from the package tests. However, I want to simply be able to type java Test. I know it's possible as I used to be able to do it, but now for some reason I cannot (I must have changed something along the way...).
Any help is appreciated.
However, I want to simply be able to type java Test
That will only work if Test is in the default package - it's as simple as that. You need to pass the java executable the fully-qualified name of the class you want to launch. There's no way round that.
Of course, you could create your own launcher which looks in the current directory for class files, finds out the fully-qualified name of the classes within those files, and launches java providing the full name and probably specifying an appropriate classpath... but that seems like a lot of hassle compared with just including the package name in the command.
You could be making the same mistake I made. So, try the following.
Here is my code for your reference.
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Once you saved this as "C:\JavaStudy\ClassA.java", try the following.
c:\JavaStudy>javac ClassA.java
c:\JavaStudy>java A.class
Error: Could not find or load main class A.class
c:\JavaStudy>java A
Hello world
c:\JavaStudy>
Note: You don't need to use " java.exe -cp . " if you have class file in the same directory from where you are executing.

What does "Exception in thread \"main\" java.lang.NoClassDefFoundError" mean when executing java .class file?

Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.

default classpath current directory anomaly

I am trying to compile and run simple Java program. This program basically prints out hello world phrase. I am not specifying -cp option and I don't have CLASSPATH environment variable. Hence the user classpath is limited only to current directory.
Now, compilation works beautifully.
rustam#rustam-laptop:~/temp/bird_test$ javac Sparrow.java
This command produces needed .class file. The weird stuff happens when I try to run .class file. The following command works good.
rustam#rustam-laptop:~/temp/bird_test$ java Sparrow
But when I try the following command
rustam#rustam-laptop:~/temp/bird_test$ java ./Sparrow
I receive the following error:
Error: Could not find or load main class ..Sparrow
WTF! i thought that symbol ./ refers to current directory.
java takes a class name as argument. It doesn't take a file path. The class name (Sparrow) is then resolved by the java class loader to a .class file based on the classpath, i.e. it looks for a Sparrow.class file in every directory and jar listed in the classpath.
Let's take an example that respects good practices, and thus doesn't use the default package:
package foo.bar;
public class Baz {
...
}
The class name of the above class is foo.bar.Baz. To execute it, you must use
java foo.bar.Baz
and java will look for a foo/bar/Baz.class in all the directories listed in the classpath. So if the classpath is set to /hello/world, it will look for the file /hello/world/foo/bar/Baz.class.

class not found error in rmic compile

I use rmi class and I have one problem. My interface's name is server and my implementing class's name is serverImpl. When i type rmic -v1.2 -classpath .. serverImpl in command line (after compiling interface and classes) it shows this error:
error: Class serverImpl not found.
I checked that serverImpl exists in the specified directory.
Put your server class and the implemented class in a folder, name it x
Add this package on top of each class package x;
Open cmd and write javac x/MyServer.java and javac x/MyImpl.java from the directory that contain the x folder.
Write rmic x.MyServer from the directory that contain the x folder.
You should have now a MyServer_stub.class and you can have a nice day :)
PS: It is important that the package name is different than RMI or any object used inside the class. Otherwise you will have object collision.
serverImpl doesn't have to 'exist in the specified directory'. You haven't specified a directory, you have specified a CLASSPATH, and serverImpl has to exist within that, under the appropriate package structure, which you also have to name correctly in the command line. So if serverImpl is in package x.y, you have to specify a CLASSPATH that contains the x/y directory, and specify x.y.serverImpl on the command line. Exactly as you do when running with the 'java' command.
But you haven't needed rmic at all for about eight years - see the class Javadoc for UnicastRemoteObject.
Please convert the slashes (/) to dots(.). It worked for me.
I have following structure
rmitest/
rmitest/MyRemoteIf.java
rmitest/MyRemoteImpl.java
javac rmitest/*.java
rmic rmitest.MyRemoteImpl
The output was :
rmitest/MyRemoteIf.class
rmitest/MyRemoteImpl.class
rmitest/MyRemoteImpl_Stub.class
rmitest/MyRemoteImpl_Skel.class
Good Luck!!
Karan

How can I create a Class file and JAR file without main function?

How can I create the class file and jar file for this coding, when I compile this program its not working because there is no main function in the program. And also I am trying in command prompt but I don't know how to set the classpath? please help me
My Coding is here
public class NewLogFields implements ILogNotify
{
public void onLog(Level level, String comment, IMediaStream stream, String category,String event, int status, String context) {
if (category.equals(WMSLoggerIDs.CAT_session) && event.equals(WMSLoggerIDs.EVT_destroy))
{
Long csBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_cs_bytes);
Long scBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_sc_bytes);
System.out.println("disconnect: csBytes:"+csBytes+" scBytes:"+scBytes);
}
}
}
Once you compiled the coding in wowza media Serever the jar file is automatically created in the library folder,see your Installation Library folder.Still you have problem Gothrough this link Wowza Quick Guide
What do you want to do?
Create a class and an jar file out of this Java code so that you can use this in another Java program?
Then you have to compile it:
java NewLogFields.java
Looks like you are unable to compile it at all. This could be because the interface ILogNotify (or the jar that contains this) is not in the classpath.
You can include the path/jar containing this interface in the classpath by using:
javac -cp .;path_to_jar_or_class NewLogFields.java
where path_to_jar_or_class is the path to the folder or jar file that contains ILogNotify.
For example, this may be something like ./logNotify.jar or ./log/
You can set use switch -cp or -classpath with javac command.
for example javac -cp path and name of jat file or class file yourjavafile.java
create the class file using the compiler: javac NewLogFileds.java
create the jar file using the jar command: jar cvf stuff.jar NewLogFileds.class
You are correct that the program needs a main() function in order to run.
add:
public static void main(String args[]) {
// code here
}
With that you could run the code with or without the jar:
java NewLogFields
or
java -cp stuff.jar NewLogFields
There are ways of creating a MANIFEST file that tells java which class to run from the jar making the last line more simple.
The link that you provided tells you how to do it:
Compile your class in the normal way.
Create a JAR file containing your class in the normal way.
Copy the JAR file into the wonza installation's lib as described in the javadoc.
Edit the startup script to add the -Dcom.wowza.wms.logging.LogNotify=... option to JAVA_OPTS ... as described in the javadoc.
The "full-path-to-your-ILogNotify-class" is actually supposed to be the fully qualified class name of your class; it is obvious from the examples.
Edit WowzaMediaServerPro-Service.conf and log4j.properties as described in the javadoc.
Restart the server.
If you put your JAR file in the directory like the instructions tell you to, you won't need to modify the classpath by changing -cp argument.
Your class doesn't need a main method because it is not a free-standing application. Rather, it is a "plugin" class that gets dynamically loaded and instantiated by the Wowza core as required. The "-D..." option and the config file change tell the Wonza core which class to try to load.

Categories

Resources