My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:
Client_1/src/filedownload/Client.java
RMI_interface/src/filedownload/Hello.java
The Client.java file is dependent on Hello.java. filedownload is the name of package.
When I compile using the following command, it works.
javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
But when I execute the class file in the Client_1/src folder using following command, it does not work.
java filedownload.Client
The error displayed is
Could not find or load main class
I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.
The code structure is
package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{
}
}
Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?
Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).
(And the questions themselves suggest various possible points of failure in your scenario so look at it.)
All your steps seems to be correct. You didn't share the Client.java code which has main method.
Make sure you follow this main method syntax:
public static void main(String[] args){
...
}
E.g. if you write main without args, it can't be found.
You need to put your classes in a separate folder, separated from your sources.
javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
(folder 'bin' must exist already)
And inside folder 'bin' execute command:
java filedownload.Client
Related
I know this question has been asked a lot, but I have tried a few suggestions and am still getting this error.
I am running the jar as follows:
java -jar MyJar-1.0.jar com.me.ldap.ActiveMain,
where my ActiveMain.java file looks like this:
package com.me.ldap;
public class ActiveMain {
public static void main(String[] args) throws Exception {
...
}
}
I have also tried simply java -jar MyJar-1.0.jar with the same Error: Could not find or load main class error. I've also looked into the class path option but I don't think that applies.
I am creating it in Intellij as a Maven project. Maven > Lifecycle > package.
In order for:
java -jar myfile.jar
to work, there must be a manifest file in the jar file that points to a main class.
In order for you to specify the main class on the command line, you need to specify a classpath, not a jar file. Like:
java -cp myfile.jar com.me.ldap.ActiveMain
You are conflating these two things. Either create a manifest that specifies your main class and use the -jar switch, or simply use the -cp switch and specify your main class on the command line.
Have you tried removing the signature files? I have seen this error pop up sometimes when due to signature files as documented in "Invalid signature file" when attempting to run a .jar and Generated JAR throws ClassNotFoundException for main class is also in the similar vein.
I'm trying to learn java, more so as a hobby than anything else, taking a different angle from my usual C# where you actually have namespaces and multiple classes in a file(not ranting or anything). However I don't seem to quite understand how the jar creation process seems to work, at least regarding the "files-input" parameter.
I am well aware that there are build tools like and,maven,etc. I just want to have a better understanding of the entire process that's all.
I'm trying to create a jar out of my 2 classes which are in 2 separate packages.
Here is the folder structure:
tryproject\packageOne\MainApp.class
tryproject\packageTwo\Greeter.class
MainApp.java
package packageOne;
import packageTwo.Greeter;
public class MainApp{
public static void main(String[] args){
Greeter greeter=new Greeter();
greeter.setMessage("Hello World");
greeter.sayHello();
}
}
Greeter.java
package packageTwo;
public class Greeter{
private String whatTosay;
public void setMessage(String whatTosay){
this.whatTosay=whatTosay;
}
public void sayHello(){
System.out.println(whatTosay);
}
}
Here is the command prompt output:
C:\Users\SomeUser>jar cfve tryapp.jar MainApp C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class
added manifest
adding:Users/SomeUser/Desktop/LearningJava/tryproject/packageOne/MainApp.class(in = 414) (out= 299)(deflated 27%)
adding:Users/SomeUser/Desktop/LearningJava/tryproject/packageTwo/Greeter.class(in = 506) (out= 333)(deflated 34%)
C:\Users\SomeUser>java -jar tryapp.jar
Error: Could not find or load main class MainApp
Caused by: java.lang.ClassNotFoundException: MainApp
I don't really understand why it doesn't find my MainApp class and I'm also curious if it is possible to build the jar this way without a pre-made manifest.
For anyone who stumbles upon this post, the reason of confusion, and the reason this didn't work out for me, is because I didn't take into account the fact that I was creating my jar somewhere else than in the root of a drive so the jar structure would look something like this:
Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class
Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class
The above is how the jar structure looks like after I created it with the following command:
C:\Users\SomeUser>jar cfve tryapp.jar MainApp C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class
Now in order to run this jar you have to change the above command so that instead of having just MainApp there you have to put the entire project structure like this:
C:\Users\SomeUser>jar cfve tryapp.jar Users.SomeUser.Desktop.LearningJava.tryproject.packageOne.MainApp C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageOne\MainApp.class C:\Users\SomeUser\Desktop\LearningJava\tryproject\packageTwo\Greeter.class
And also you have to modify the package of each .java class file to resemble the structure I just modified in the command. So instead of having just package packageOne; or package packageTwo; you need to rewrite them like this package Users.SomeUser.Desktop.LearningJava.tryproject.packageOne; and package Users.SomeUser.Desktop.LearningJava.tryproject.packageTwo;. The same goes for import.
Now if you modified all your .java files and recompiled them you should be able to run the jar using the command instruction that I mentioned earlier.
But a better solutions, and an easier one is like #Prashant Gupta mentioned above.
For instance just use:
jar cfve tryapp.jar packageOne.MainApp -C C:\Users\SomeUser\Desktop\LearningJava\tryproject\ .
But it will get all the files in that directory so maybe just have a separate directory for binary files or all file you need for your application and another for the source files.
For some reason, I can't get java to run my program. Whenever I try I get the message
"Error: Could not find or load main class Project"
In Command Prompt I type cd Documents since the file is in my Documents folder, type
javac Project.java
then
java Project
to try and run it but I get the above error message.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project
{
public static void main(String[] args)
{
Code and stuff
}
}
There's a fair bit of code that I left out but I think this is the part that's messed up. Let me know if you need to see the rest of the code and I'll edit this.
Change
java Project
to (assuming Project.class is in your current folder)
java -cp . Project
as it is, you aren't setting a class-path.
You have add the path of .class files in classpath during execution
Run following command:
java -classpath C:\Users\DELL\Documents Project
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.
I have written two simple Java classes (one of them containing "main()", and the other called by "main()").
Class #1 (containing "main()"):
package daniel347x.outerjar;
import daniel347x.innerjar.Funky;
public class App
{
public static void main( String[] args )
{
Funky.foo();
}
}
Class #2 (called by "main()"):
package daniel347x.innerjar;
public class Funky
{
public static void foo()
{
System.out.println( "Funky!" );
}
}
The above classes appear in different project root folders, and use Maven as the build system (each project has its own POM). The pom.xml file for the main project includes the proper entry to add daniel347x.outerjar.App as the main class, and it properly includes the dependency on daniel347x.innerjar. Both projects build successfully into JAR files.
I use NetBeans to wrap these as Maven projects (in fact, I used NetBeans to create both projects). When I run the main project from within NetBeans, it runs successfully and I see Funky! as the output.
However, when I attempt to run the main class straight from the Windows command line (cmd.exe), passing the JAR file containing Funky on the command line's classpath, as such:
java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar" -jar "P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar"
... I receive the dreaded NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: daniel347x/innerjar/Funky
at daniel347x.outerjar.App.main(App.java:7)
I have carefully confirmed that, inside the innerjar JAR file noted above containing Funky, that the path structure is daniel347x\innerjar and that inside the innerjar folder is the Funky.class file - and this file looks correct within a HEX editor where I can see the ASCII strings representing the name of the class.
The fact that the class can't be found defies my understanding of Java, which I thought allows you to pass JAR files as a -classpath parameter and it will be able to find classes inside those JAR files.
This very basic point has me flummoxed - an answer that explains what I am doing wrong would be greatly appreciated.
The classpath is ignored when using the -jar option. A way to run your app would be java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar";"P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar" daniel347x.outerjar.App
Perhaps a better approach would be to add a manifest file to the Jar that specifies the class path of the dependent Jars by relative paths. Then..
java -jar "P:\_Dan\...\outerjar-1.0-SNAPSHOT.jar"
..should do it.
Double clicking the main Jar will also launch it. That is mostly useful for GUIs.