Java FrameDemo Error: Could not find or load main class - java

I'm starting to learn Java, and I am attempting to run the FrameDemo tutorial code found here: https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
I can compile fine with javac FrameDemo.java but when I try to run it with java -cp . FrameDemo I get this error:
Error: Could not find or load main class FrameDemo
I thought it was my classpath so I tried java -cp D:\projects\framedemo FrameDemo and got the same error.
Then I tried recompiling with javac -cp . FrameDemo.java and javac -cp D:\projects\framedemo FrameDemo.java and still got the same error when running java -cp . FrameDemo and java -cp D:\projects\framedemo FrameDemo. I also tried running java -cp . FrameDemo.class just for the heck of it.
Always I get Error: Could not find or load main class FrameDemo
What am I doing wong here? I've just downloaded and installed the latest JDK. The source code is exactly what is in the demo link above.

move all your .class files to a folder 'components'.
Then run this:
java components/FrameDemo
or
remove package components; from your source file, recompile and run :
java FrameDemo

Related

Class not found when running program from command line

Here is the structure of my project:
MyProject
-lib
-mp3agic-0.9.0.jar
-src
-com.company
-Main.java
Inside Main, I import the content of mp3agic:
import com.mpatric.mp3agic.*;
From MyProject, I compile the class using:
javac -cp "lib/*" src/com/company/Main.java
From MyProject/src, I try to run the program with:
java -cp . com/company/Main
and I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mpatric/mp3agic/InvalidDataException
Apparently, the jdk can not find the dependencies from the lib folder when running the class, albeit this problem doesn't occur when compiling.
I guess there's a problem with the syntax of the command, but I just can't find it.
Can you please help me?
EDIT: I solved the problem, moving the lib folder under src and then running:
C:\MyProject\src>javac -cp "lib/*" com/company/Main.java
C:\MyProject\src>java -cp .;"lib/*" com/company/Main
Now, the code compiles and executes without any issue.
HOWEVER, if I leave lib under the project root, this is what happens:
C:\MyProject>javac -cp "lib/*" src/com/company/Main.java
C:\MyProject>java -cp .;"lib/*" src/com/company/Main
Error: Could not find or load main class src.com.company.Main
WHY?
You should use maven or gradle or you'll have to say which console are your using, and which operating system -if you don't know what a console is, you'll first have to google for that before messing with javac directly- But if you insist:
for Linux, OSX, any UNIX-like:
javac -cp ".:lib/mp3agic-0.9.0.jar" src/com/company/Main.java
for Windows:
javac -cp ".;lib/mp3agic-0.9.0.jar" src/com/company/Main.java
To run under windows:
java -cp .;lib\mp4agic-0.9.0.jar com.company.Main
You may need to use slash (/) instead of backslash depending of which console you're using. Or just try "/" or "\" and see what works for you.
If you still get class not found exception, then the issue is not related with classpath or java command but on your code (hard to say, without seeing the code).
java.exe -classpath {path} Main
I think you have to add classpath also and when run the program(not only compile)

Java Swing Program is not running on command prompt

My java swing code compiled successfully but its not executing from cmd
package swing_1;
public class JavaCalculator implements ActionListener{
the above class contains the main method
when i'm compiling there's no error showing javac JavaCalculator.java
but when I trying to execute javac JavaCalculator.java
its showing
Error: Could not find or load main class JavaCalculator
Caused by: java.lang.NoClassDefFoundError: swing_1/JavaCalculator (wrong name: JavaCalculator)
Its seems you are missing a manifest. Try calling it this way:
java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2
where the latter part is the location of your Class that contains the main method
That is because you should compile in the directory where the subdirectory swing_1 resides:
javac swing_1/JavaCalculator.java
java -cp . swing_1/JavaCalculator.class
Start with an IDE like NetBeans or IntelliJ (community) or eclipse.

launching Jython console with Tomcat 8 servlet

If I launch Jython like this:
"java -cp jython.jar"
or
"java -cp jython.jar org.python.util.jython"
works ok and launches Jython REPL
if I include another jar like:
"java -cp jython.jar:servlet-api.jar org.python.util.jython"
gaves me the folowing error:
Error: Could not find or load main class org.python.util.jython
Any ideas how can I fix the error?
(both .jar(s) are available and in the current folder)
java -cp jython.jar:servlet-api.jar org.python.util.jython works for me on OpenJDK 7u221 and Jython 2.7.1.

JGroups not finding or loading main class

So I'm currently working through the official JGroups tutorial here
I compile the class, specifying the correct JGroups class path, however when I run the SimpleChat, I get the following error message.
User#0001-Macbook:~/Desktop/JGroupsTutorial/src$ javac -cp jgroups-3.4.0.Final-2.jar SimpleChat.java
User#0001-Macbook:~/Desktop/JGroupsTutorial/src$ ls
SimpleChat.class SimpleChat.java jgroups-3.4.0.Final-2.jar
User#0001-Macbook:~/Desktop/JGroupsTutorial/src$ java -cp jgroups-3.4.0.Final-2.jar SimpleChat
Error: Could not find or load main class SimpleChat
You need to add the location of your .class file to your classpath.
For Mac/Linux:
java -cp .:jgroups-3.4.0.Final-2.jar SimpleChat
For Windows:
java -cp ".;jgroups-3.4.0.Final-2.jar" SimpleChat

Could not find or load main class when I add external libraries

Hey I got this problem when running a java program through my cmd.
I use to external libraries : lwjgl.jar and slick-util.jar
when I compile
javac -d bin -sourcepath src -cp ;lib/lwjgl.jar;lib/slick-util.jar src/*.java
everything works fine
but when I try to run it
java -cp bin .;lib/lwjgl.jar;lib/slick-util.jar; Game
I get this error
Error: could not find or load main class .;lib.lwjgl.jar;lib.slick-util.jar;
Replace the space in the run command classpath with a classpath separator
java -cp bin;.;lib/lwjgl.jar;lib/slick-util.jar; Game
^
as an improvement to the run command you could use a classpath wilcards, for example
java -cp ".;bin;lib/*" Game
It is important to use quotes under Windows for this option

Categories