In NetBeans 8.0.2 I've done a simple "Hello World" class that compiles and everything is fine.
public class OOP_HW3 {
public static void main(String[] args) {
System.out.println("Hi ");
}
}
On the other hand when I navigate to this folder with console and run:
$ javac OOP_HW3.java // OK
$ java OOP_HW3
Exception in thread "main" java.lang.NoClassDefFoundError: OOP_HW3 (wrong name: oop_hw3/OOP_HW3)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
....
I can easily run this file with console in any other directory, but here in the NetBeans project gives me error. What am I missing ?
Run the following commands from where you are running current command like:
cd ..
java oop_hw3.OOP_HW3
Since you are using package, you need to be on one top i.e. parent directory and then run with fully qualified package name followed by class where you define main method.
Remove the package definition from the top of your java file.
Related
I'm trying to import a .jar file in my project whitout using IDE and the way I'm doing everything works fine until I run the program. I'm getting the following error message when I run it.
So, I created this bar-project folder which I created the .jar file.
bar-project/resources
bar-project/src/com/bar/packmain/Bar.java
Bar.java:
package com.bar.packmain;
public class Bar {
public Bar() {
System.out.println("Bar.");
}
}
At the folder bar-project/src/ I'm compiling like this:
javac -d . com/bar/packmain/Bar.java
To create the jar file I'm using the follwing command:
jar -cvf Bar.jar com/bar/packmain/Bar.class
Then I move this .jar to my other project called foo-project.
foo-project/resources
foo-project/library/Bar.jar
foo-project/src/com/foo/packmain/Foo.java
Foo.java:
package com.foo.packmain;
import com.bar.packmain.Bar;
public class Foo {
private Bar bar;
public Foo() {
new Bar();
}
public static void main(String[] args) {
new Foo();
}
}
At the foo-project/src/ folder I'm compiling and running like this:
javac -cp .:../library/Bar.jar -d . com/foo/packmain/Foo.java
java com.foo.packmain.Foo
Exception message:
Exception in thread "main" java.lang.NoClassDefFoundError: com/bar/packmain/Bar
at com.foo.packmain.Foo.<init>(Foo.java:10)
at com.foo.packmain.Foo.main(Foo.java:14)
Caused by: java.lang.ClassNotFoundException: com.bar.packmain.Bar
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
So, my question is how can I fix this?
You compiled the project without an error:
javac -cp .:../library/Bar.jar -d . com/foo/packmain/Foo.java
But your command to run your application doesn't include the Bar.jar in classpath.
You have to include your JAR file here, too:
java -cp .:../library/Bar.jar com.foo.packmain.Foo
This is a good way to learn how these things work at the lower level and you get a better understanding what higher levels tools like an IDE or Maven really do.
I have a java file ComPac.java with the below code:
package com;
public class ComPac{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The file is located at the path : /home/ec2-user/java_c
To compile this file, I ran javac Compac.java, and the class file was generated.
Now its turn to run the class file.
So I did java ComPac(screenshot below)
Understandably, I got the error Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac).
This I am assuming is because the java file has the package com declared in it.
So instead I tried, java com.ComPac and expected it to work(screenshot below).
But I got the error: Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac.
So how do I run this? and what exactly is the logic for running when it comes to packages in java?
Java used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto)
OS used- Amazon Linux 2
put the class in a folder called "com"
in a bash shell it's then:
$ java com/ComPac
(from the folder containing "com", not inside of "com")
If you are using Java 11 then you don't need to first compile java file and then run the class file. You can directly run the java file using just
java .\test.java
test.java
package com;
public class test{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
command:
java .\test.java
Output:
Hello World
The correct way of doing it as follows:
javac -d . ComPac.java
The switch -d . asks the compiler to place generated class files at the current directory as . stands for the current directory. Learn more about the switches by simply using the command javac
Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, com has been created and ComPac.class has been placed inside this directory. In order to execute the class, you can now use the following command:
java com.ComPac
If I compile and run the program. I don't have any issues.
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
If I add the line package ch01.sec01; it complies correctly with javac. However when I try to run it using java I get:
Error: Could not find or load main class HelloWorld
I have tried the following.
export CLASSPATH=/usr/lib/jvm/java-9-openjdk-amd64/bin:/usr/lib/jvm/java-1.9.0-openjdk-amd64/bin
That is why when you use a package in your code, that path must be the actual path of your java file (That means that your code is supposed to be in a directory called sec01 which is inside directory ch01).
With that being set, when running code inside a package, you need to include the path in the command. To do so, after you have compiled your code with javac, navigate to the root of the path (outside ch01 directory) and type
java ch01.sec01.HelloWorld
This should work.
I know this question has already been answered many times, but unfortunately I couldn't find the right answer to my questions.
below is my package structure and inside my package I have SimpleTest.java
d:\junit\src\junitfaq\SimpleTest.java
inside d:\junit\src> i tried to compile SimpleTest.java and it successfully compiled using the command below.
d:\junit\src>javac junitfaq/SimpleTest.java
but when i try to run the program using command line below
d:\junit\src>java junitfaq.SimpleTest
this error occured. Error: Could not find or load main class junitfaq.SimpleTest
I tried running it by accessing junitfaq package by using this command
d:\junit\src\junitfaq>java -cp . SimpleTest
the program run perfectly. A little help would be much appreciated.
Have you declared your SimpleTest class to be a member of the junitfaq package? If you have, you should be able to run it from the src directory like java junitfaq.SimpleTest but you should get an error like this if you try to run it from within the junitfaq directory: Exception in thread "main" java.lang.NoClassDefFoundError: SimpleTest (wrong name: junitfaq/SimpleTest)
Make sure your SimpleTest class starts with package junitfaq;
Edit: Here's a working example incorporating the comments below.
login#domain:~/temp> mkdir src
login#domain:~/temp> cd src
login#domain:~/temp/src> mkdir junitfaq
login#domain:~/temp/src> nano junitfaq/SimpleTest.java
The contents of SimpleTest.java are as follows when I exit nano:
package junitfaq;
public class SimpleTest {
public static void main(String[] args) {
System.out.println("Test");
}
}
login#domain:~/temp/src> javac junitfaq/SimpleTest.java
login#domain:~/temp/src> java junitfaq.SimpleTest
Test
It sounds like you have a classpath problem; you should double-check the location of your class file, the directory/package structure, the location from which you're trying to run the java command, and any classpath specified during execution.
For example, the following works for me:
$ mkdir junitfaq
$ cat >junitfaq/SimpleTest.java
package junitfaq;
public class SimpleTest
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
^D
$ javac junitfaq/SimpleTest.java
$ java junitfaq.SimpleTest
Hello, world!
$ java -cp . junitfaq.SimpleTest
Hello, world!
$
Not to belabor the obvious, but I noticed a spelling typo in one of your comments - you should also double-check that you're running the command as intended.
trialYou could try putting your java and class files together with your libaray folder (if any) in a new file called "trial" in your C:\ directory then compile existing java file using the following->
C:\trial> javac -cp .;library folder* SimpleTest.java
(then)
C:\trial> java -cp .;library folder* SimpleTest
Let me know how you get on!
I am a beginner in Java but not in OOP I have some experience in C and C++ and PHP5
For short I have "hello world" program for test
package com.tutorial.helloworld;
public class helloWorld {
public static void main(String[] args) {
System.out.print("hello world!!!\n");
}
}
When I compile in console with javac compile with no error but when I run Java helloWorld
says
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name:
com/tutorial/helloworld/helloWorld) and much more code
In eclipse run ok. If I delete package statement and compile manually will run ok. but if I keep package statement throws that error.
should I put the class file in a subdirectory com/tutorial/helloworld and is that ok how should I run from terminal and from what directory?
I am on mac os x and I am use to type code in edit and compile and run from console
than run in a ice. I cannot make eclipse to work for c++ (c++ ide)and because of that I try to stick on the console with all languages I know or I learn.
In Java the class name consists of package name + the class's "first name". Therefore write
java com.tutorial.helloworld.helloWorld
You must also know where your .class files are. You must be in the directory containing the com directory for this to work, where the .class file finds itself inside com/tutorial/helloworld directory.