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.
Related
I have a problem.
I try to run my java program with the library.
When I use
java com/myProg
it works.
But when I try to pass classpath it can't find or load main class
java -cp com/lib/lid.jar com/myProgram
error:
Error: Could not find or load main class myProgram
Caused by: java.lang.ClassNotFoundException: myProgram
Try specifying after the -cp option only the root of the working directory and don't include the package name there. If you're running the java command from the dir of /com 's location, -cp . com/myProgram will be enough,otherwise you'll need the fully qualified name after the classpath.
Not sure of the necessity of the .jar file, you stated that first you ran only via
java com/myProg.
I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214
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
I have problem running a basic helloworld application in Java form my command-line in widnows 7. I can run it in Java.
Here is my code(in NetBeans):
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I have set C:\Program Files\Java\jdk1.8.0_20\bin; on my PATH variable in the Windows Environment.
When running :
javac HelloWorld.java
the HelloWorld.class is built successfully.
However in the next step when I run:
java HelloWorld
I get he following error:
Error: Could not find or load main class HelloWorld
Under my program source root directory I can see these two file:
. HelloWorld.class
. HelloWorld.java
What am I missing please?
You should specify fully qualified class name. That is, you need to run it like that: java helloworld.HelloWorld.
what you need to do is as you have
package helloworld;
and you are trying to execute it from the commandline
do the following steps
First open the terminal or cmd and browse to the folder helloworld.
Example if your helloworld folder in in f:/helloworld open the terminal and browse upto f:/(don't go inside helloworld)
then compile the class as javac helloworld/HelloWorld.java
and try executing the class as java helloworld.HelloWorld
the class name was not fully qualified, try java helloworld.HelloWorld
the .classfile should not be in the directory the java command is run from.
you forgot the helloworld package. So you have to enter java helloworld.HelloWorld to make it work. Next time please use a different package name than the java file, so there is no confusion. :)
I have been mostly using eclipse so far. Now I'm trying to run java from terminal but I have a problem with packages.
This is my Main.java file:
package main;
class Main {
public static void main(String[] args) {
System.out.println("it's working");
}
}
I compile this using javac Main.java and then run with java Main which gives me:
java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Main. Program will exit.
When I remove package Main everything works fine. What am I missing?
java -version gives:
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
You need to run the java command up one directory level and give it in the fully qualified package name, eg: java main.Main
See How the Java Launcher Finds User Classes to learn how this works.
You can use this command:
java main.Main
Make sure the main (lowercase) package directory is on the classpath.
It is possible that your classpath is not set correctly.
Since you gave your .java file a package it is unnamed no longer.
An example:
java -cp ./package1/ main.Main //from the current directory and
//if main package is contained in package1
You need to fully qualify the class name.
For future reference if you want to run from the command line you must stop the indirection (for lack of a better term) at the package level.
Say your class was in the package package1.package2.Main.java
I would run it like so java -cp /blah/blah package1.package2.Main
Compile
Windows:
javac main\Main.java
Mac:
javac main/Main.java
Run
java main.Main
If you add package Main, then you must put your source file in folder Main/Main.java. After that you can compile. When you run the program, go to Main folder using "cd", then write java -cp Main.Main
See my question similiar to yours noclassdeffounderror
try this...
In window , you just compile the code as
javac - d . Main.java
then a package(folder) with the name you specified in your class is created (in your code, package with name "main" is created) in the same path where your program reside...
Then you just run the program as
java main.Main
or
java main/Main