I am trying to import a package found at /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar. I am using import ptolemy.plot.* and compiling with javac -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar The Class.java. When I run (using java -cp ...same... TheClass) I get Error:Could not find or create main class TheClass.
When I take away the -cp from the java call the Could not find or create error goes away...
I know this must be something simple but I can't figure this out!
If you specify that the classpath is a single jar files -- as you seem to be doing -- then Java won't find any classes outside of that jar file. You need your classpath to include both the jar file and the location of your compiled classes. You can use "." to mean the current directory; i.e.,
java -cp .:/home/jirwin/ptplot5.8/ptolemy/plot/plot.jar TheClass
Note the "dot colon" prepended to the beginning of the classpath.
You need to put your full package name in front of your .class when you run it with java. Otherwise it looks in the wrong place, or something. (I don't understand java well enough to give you the "why" but it's the idea.)
java -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar {package}.TheClass
Related
I have been searching the web trying to find the answer to my question, but everywhere I look seems to have too complex of a solution for a beginner like me. I have been working on this project, and just now realized that I should've made a package, or something like that. The thing is though, my program was working fine until I started dabbling with it, and now it won't work at all. I am getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort. class
Caused by: java.lang.ClassNotFoundException: BubbleSort.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:672)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at ``java.lang.ClassLoader.loadClass(ClassLoader.java:638)
Could not find the main class: BubbleSort.class. Program will exit.
Here's how my "path" looks, if I am not mistaken. I am connected to my school's Z: drive through a remote connection, and from there I have a folder called myFirstname_Lastname_A4,
which then leads me to another folder called sortingzz which I believe is supposed to have only my source files, but it also ended up with my class files in there whenever I compiled. So here's what I am doing to compile.
cd myFirstname_Lastname_A4/sortingzz
javac *.java (Works fine, this is where I end up with my Class files inside of my sortingzz folder)
java * (This is where I get the error)
I am pretty sure I am just trying to run the program wrong. Before I started messing around with stuff I wasn't ready for, I used to just run the file with my main function in it, like this
javac SortingImplementation.java
java SortingImplementation
And that for the most part worked fine, but I started having trouble calling certain classes from other classes, so thats when I found out I was suppose to do the packaging and importing stuff.
In case that is the issue, I have on the top line of every source file:
package sortingzz;
and I am importing like this:
import sortingzz.*;
This is correct, right?
UPDATE:
I decided to give up on class pathing and trying to package everything, because as usual, I am getting responses that are way over my head, and to be honest I don't think it is necessary.
After removing package and importing from everything, and once again compiling using javac *., it actually compiles this time. However whenever I try to run my class with the main in it, SortingImplementation, it tells me that
Could not find the main class: SortingImplementation. Program will exit.
I don't get it, I am looking at the SortingImplementation.class right now, with all the other classes and java files, so I am not sure what it's trying to do.
javac *.java is fine. This will compile your files. However, you only need to run the file with your main method in it: java MainClass
You say that you are using packages to organize the classes. In this case you need to set the class path using the -cp flag.
javac -cp /path/to/parent/of/package classname.java
and
java classname
Also, your main class should be declared public and should have a main()
NoClassDefFoundError occures when a class was recognised in compile time but was not available during runtime.
So the JVM can't find your class in the classpath.
using -cp flag to specify where your package is should work.
the commanc javac *.java compiles all found java files to corresponding .class files. If you all your classfiles are in the same folder, which they should, you just run your regular java SortingImplementation command.
java * would, a bit depending on your OS, yield in an undesired command. For instance, on Linux it would be expanded by the OS to java SortingImplementation.java SortingImplementation.class BubbleSort. The last one is a directory, which ofcourse is not an executable class.
I know this has come up a number of times, but previous responses just don't seem to help.
My environment variables are :
CLASSPATH C:\Program Files\Java\jre7\lib;C:\Program
Files\Java\jdk1.7.0_15\bin;
PATH C:\Program Files\Java\jdk1.7.0_15\bin;
When moving to the directory as follows C:\Users\Oli\My Documents\java I can compile using javac, but cannot runt he program using java. I know its most likely got something to do with environment variables but I cannot get it to work. P.S the error is "could not find or load main class"
Any help would be appreciated.
CLASSPATH is the place where JRE looks for classes. You've set your CLASSPATH to a value and expect to run the class from current Directory, which won't work.. for instant solution you may use
java -cp C:\Users\Oli\My Documents\java ClassName
Or undo setting CLASSPATH. Default CLASSPATH is current directory
Lets assume that your ".java" file default package ( no package defined) survivies in "C:\Src"
You dont need to set the CLASSPATH in this case.
cd C:\Src
javac MyJava.java
java MyJava
If with package say com.test
cd C:\Src
javac com\test\MyJava.java
java com.test.MyJava
However if you are not in the same folder as Source files and want to run from anywhere
set CLASSPATH=%CLASSPATH%;C:\src
javac MyJava.java or javac com\test\MyJava.java
and
java com.test.MyJava or java com.test.MyJava
Unset CLASSPATH and just use the default one provided by the JVM. Here is a link to the Java Tutorial that covers the environment variables.
Seems like the problem is not in the path...
Does your code use the 'package' statement? (i.e. package my_package;)
If so, go to 'java' directory and execute:
java my_package.MyClass
where 'my_package' is the name of... the package, and MyClass is your compiled .java file (without the .class extension).
Good luck.
I have a Java program called Main.java, it is located in the following directory:
/home/user/program/Main.java
When I try to run Main.java from the 'program' directory, everything goes ok, I use this line:
/home/user/program$ java Main
But when I try to run Main.java from the home directory :
/home$ java /home/user/program/Main
I get :
Exception in thread "main" java.lang.NoClassDefFoundError: /home/user/program/Main
Caused by: java.lang.ClassNotFoundException: .home.user.program.Main
What is the cause of this error?
This is due to your classpath, which will default to the current directory. When you run java Main from /home/user/program it finds the class in the current directory (since the package seems to be unset, meaning it is the default). Hence, it finds the class in /home/user/program/Main.class.
Running java /home/user/program/Main from /home tries to find the class in the classpath (the current directory) which will look in /home/home/user/program expecting to find the file Main.class containing a definition of the Main class with package .home.user.program.
Extra detail: I think the java
launcher is trying to be nice by
converting /-notation for a classname
to the .-notation; and when you run
java /home/user/program/Main it is
actually running java
.home.user.program.Main for you. This
is because you shouldn't be specifying
a file, but a fully specified
classname (ie including package
specifier). And when a class has a package
java expects to find that class within a
directory structure that matches the package
name, inside a directory (or jar) in the
classpath; hence, it will try to look in
/home/home/user/program for the class file
You can fix it by specifying your classpath with -cp or -classpath:
java -cp /home/user/program Main
Because its looking for the class using the fullname you give (/home/user/program/Main). You should only look for the Main class but using the good classpath :
java Main -cp /home/user/program
Which means it'll search the Main class in the given set of paths
Your 2nd command version does not know where to find the classes.
You need to provide the so called classpath
/home$ java -cp userprogram Main
Because of what you say I conclude this:
Main is in "top" (root) package
And when you execute java you must indicate the classpath, it is, the root directory where your pakage and classes structure is located.
In your case it is the very /home/user/program. And I guess your classpath is defined as "." (the dir you are located at). When you call java from home the classpath is being taken erroneosly.
If you want to call your main using a different package declare the package at the top of the class:
package user.program;
And set the classpath to /home (or execute java from that dir).
Next call java this way:
java user.program.Main
using dots because its a full class name (indicating packages). That is translated to dirs concatenating classpath + package + class. By example:
/home
user.program -> user/program/
Main -> Main.class
Good luck!
The problem is that if you call java /home/user/program/Main the package Main is in is meant to be home.user.program, which I assume is not true for Main (I assume it's in the default package, i.e. none at all). Is there a package declaration at the top of Main?
I'd suggest to use the classpath suggestions in the other answers.
This works for me:
java -cp /home/user/program Main
just a while ago faced this kind of error of (NoClassDefFoundError). I imported some third party library in my android app using eclipse env. I got this error during a runtime - some class from this third party library couldn't be found and a result of this NoClassDefFoundError was thrown, despite the mentioned library correctly appeared in classpath, so I really didn't know what else can be done to solve this problem.
While playing with "Order and Export" tab within "Java Build Path", I put my imported third party library to the top of the list of all libraries in my project and checked its checkbox - this solved the problem
I came across this same error when trying to compile and run it. The book, "Head First Java" explains and addresses this problem appropriately. Here is a screenshot from the book for your reference.
Hope its helpful.
I'm trying to run .class file from command line. It works when I manually move to the directory it's stored in, but when I try something like this:
java C:\Peter\Michael\Lazarus\Main
it says it can't find the main class. Is there any solution to this other than making a .jar file (I know that .jar is the best solution, but at this moment isn't the one I'm looking for)?
The Java application launcher (a.k.a java.exe or simply java) supports up to four different ways to specify what to launch (depending on which Java version you use).
Specifying a class name is the most basic way. Note that the class name is different from the file name.
java -cp path/to/classFiles/ mypackage.Main
Here we start the class mypackage.Main and use the -cp switch to specify the classpath which is used to find the class (the full path to the class mypackage.Main will be path/to/classFiles/mypackage/Main.class.
Starting a jar file.
java -jar myJar.jar
This puts the jar itself and anything specified on its Class-Path entry on the class path and starts the class indicated via the Main-Class entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).
Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):
java --module my.module
Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:
java MyMain.java
This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).
This feature was developed as JEP 330 and is still sometimes referred to as such.
For your specific case you'd use option #1 and tell java where to look for that class by using the -classpath option (or its short form -cp):
java -classpath C:\Peter\Michael\Lazarus\ Main
If your Main.java contains the entirety of your source code (and it is in the same directory), then you can use option #4, skip the compile step and directly compile-and-execute it:
java c:\Peter\Michael\Lazarus\Main.java
Assuming that Main.class does not have a package declaration:
java -cp C:\Peter\Michael\Lazarus\ Main
Java looks for classes in a "classpath", which can be set on the command line via the -cp option.
I just had the same issue, I tried running java hello.class, this is wrong.
The command should be java hello.
Do not include the file extension. It is looking for a class file, and will add the name on its own.
So running 'java hello.class' will tell it to go looking for 'hello.class.class' file.
Try this:
java -cp C:\Peter\Michael\Lazarus Main
You need to define the classpath.
I'm trying to figure out how organize source and class files working with packages. I found a very useful tutorial. But I still have some questions.
As far as I understood it is a good practice to have an isomorphism between name of packages and name of the directories where elements of a package are stored. For example if I have a package named aaa.bbb.ccc which contains class ddd it is a good practice to have a class file called "ddd.class" and located in "$CLASSPATH/aaa/bbb/ccc/". Did I get it right?
If it is the case, will Java compiler put *.class files into the correct directory automatically?
I was not able to get this behavior. I set the $CLASSPATH variable to "/home/myname/java/classes". I executed javac KeyEventDemo.java which contains package events;. I expected that javac will create a subdirectory events under /home/myname/java/classes and put the KeyEventDemo.class in this subdirectory.
It did not happen. I tried to help to javac and created "events" subdirectory by myself. I used javac again but it does not want to put class files under "/home/myname/java/classes/events". What am I doing wrong?
You need to use the -d option to specify where you want the .class files to end up. Just specify the base directory; javac will create any directories necessary to correspond to the right package.
Example (based on your question):
javac -d ~/java/classes KeyEventDemo.java
For example if I have a package named
"aaa.bbb.ccc" which contains class
"ddd" it is a good practice to have a
class file called "ddd.class" and
located in "$CLASSPATH/aaa/bbb/ccc/".
Did I get it right?
That's not "good practice" - this is how the Sun JDK expects things to be. Otherwise, it will not work. Theoretically, other Java implementations could work differently, but I don't know any that do.
If it is the case, will Java compiler
put *.class file into a correct
directory automatically?
Yes
What am I doing wrong?
The source code must also already follow this structure, i.e. KeyEventDemo.java must reside in a subdirectory named "events". Then you do "javac events/KeyEventDemo.java", and it should work.
It is not only good practice but a must in most cases.
consider a Java class named:
com.example.Hello
If you store it on the filesystem, it has to got to
/path/to/my/classes/com/example/Hello.java
The compiler (or at least the vast majority) will create the class file at
/path/to/my/classes/com/example/Hello.class
Personally I would not use the CLASSPATH variable to set the path but the -cp option on java. A call to the above "application" could be done with:
java -cp /path/to/my/classes com.example.Hello