InvalidModuleDescriptorException when running my first java app - java

I have started learning Java and encountered a problem when trying to run my first program as given below:
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello world!");
}
}
On Eclipse Photon I encounter this error when running it:
Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\Thomas\eclipse-workspace\HelloWorld\bin
Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)
I looked and there is my .class file in bin directory and my .java in the src directory.
Is that normal? How do I fix that?

I was getting the same error. Deleting the module-info.java file solved it for me.

It seems that you haven't created a package. My usual procedure in Eclipse is:
Create a new Java project
Inside that project: Create a new package
Inside that package: Create a new Java class
Eclipse will help you a lot with the settings. Then just copy your code into that class and hit the 'start' button.

by removing module class problem solved for me in eclipse

i moved my main class and sample.fxml file to a new package that throws this error below
"Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\Thomas\eclipse-workspace\HelloWorld\bin
Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)"
i moved my class file and fxml file to normal position. it fix my problem. but i have to find why it happends. cheers....

Executing the project from another workspace solved for me in Eclipse.

create a package and refactor your Main class in that package in your case its HelloWorld then it will run fine.
To understand how modules work in java please watch this spectacular video MODULES IN JAVA

I've solved it do according to this steps:
right click on your main project name
choose new>>package
in the opening window fill name field and then choose (radio button) "create pakage info"
then click ok and in package explorer drag your class under the new package which you created, then run it.

Delete module-info.java from your project and run ,it should work. Or Create package and then create class inside package.

Related

IntelliJ cucumber .ClassNotFoundException

I don't know if this question has been asked/answered yet. If so, I'm sorry. But I'm new to IntelliJ and there's a lot of foreign words I yet need to understand.
My problem is as follows:
I installed the complete IntelliJ package, created a new Java project and created a new class inside my src folder:
package de.itsme.hello;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
So I try to run it and I get this Error:
Error: Could not find or load main class cucumber.api.cli.Main
Caused by: java.lang.ClassNotFoundException: cucumber.api.cli.Main
I tried adding cucumber-core:4.0.1 to the project's dependencies but then I got another exception:
Exception in thread "main" cucumber.runtime.CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH.
at cucumber.runtime.BackendModuleBackendSupplier.get(BackendModuleBackendSupplier.java:39)
at cucumber.runner.SingletonRunnerSupplier.createRunner(SingletonRunnerSupplier.java:38)
at cucumber.runner.SingletonRunnerSupplier.get(SingletonRunnerSupplier.java:32)
at cucumber.runtime.Runtime.run(Runtime.java:75)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
I don't need cucumber or at least not for now. Is there a way to make it either work or remove it from the project?
Thanks in advance!
Maybe you have the packet JDK installed incorrectly and this produces you an error. Or maybe your classspath is incorrect.

Java Error in SizeOf library

I saw this link which uses Instrumentation to calculate the size of objects during runtime. I decided to try this library since It can be really helpful in determining the size of big data structures.
So I wrote the following code in a new project named TrySizeOf using NetBeans IDE:
package trysizeof;
import net.sourceforge.sizeof.SizeOf;
public class TrySizeOf {
public static void main(String[] args) {
String s = "abc";
System.out.println(SizeOf.deepSizeOf(s));
}
}
After that I created a folder named lib inside my project, and placed SizeOf.jar in it. Then under Project->Properties->Run I placed the following parameter:
-javaagent:/home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
However, when I attempt to run my project I get the following error:
Error occurred during initialization of VM
Error opening zip file or JAR manifest missing : /home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
agent library failed to init: instrument
Can any one help me with this? or maybe explain what did I do wrong?
UPDATE:
I found the error, it was a problem with the upper and lower cases. When I started the project for the first time I made a little upper and lower case typo. When I paid attention to it I fixed it, but I kept getting the same error (However the path was fixed). Now, I tried using clean and build and the project worked. It was that when I do a clean and build manifest file gets updated which is used to specify the path of premain method. When I tried clean and build the file got updated and the project finally worked. Thanks for the comment about a typo in my post, which somehow made me think about this.

Java: Eclipse: Cannot Find Main Class

I had a project that was compiling and running fine. I had also exported the build.xml file without issue. However, today, I clicked Project --> Clean..., and the project will no longer run. It raises the error:
Error: Could not find or load main class com.bar.Foo
Is there any way I can undo this?
The answer is probably no, you cannot "undo" this. You can probably fix it by building your project, making sure that the project/class exists and that it contains a public static void main(String ... args) method.
Perhaps the file that you're trying to run was removed from the src folder.

Pig UDF in Java: Error 1070

I'm trying to build my first Pig UDF in Java and am having trouble calling the function when building with Eclipse (I have the pig 0.10.0 jar file in my class path). The source file is in /com/foo/bar/pig/IsInternal.java and the class file is placed in /bin/com/foo/bar/pig/IsInternal.class by Eclipse.
My code looks like this:
package com.foo.bar.pig;
// ... imports ...
public class IsInternal extends FilterFunc {
public Boolean exec(Tuple input) throws IOException {
// ... code here ...
return true; // or false
}
}
After I compile it, I run jar -cf PiggyBank.jar BiggyBank from just outside the project directory to package it all up into a JAR. When I run Pig, I try the following in the grunt shell:
REGISTER /full/path/to/PiggyBank.jar
DEFINE isInternal com.foo.bar.pig.IsInternal();
A = LOAD '/some/file/in/hdfs' USING PigStorage();
B = FILTER A BY isInternal($1);
At that point I get the following error:
ERROR 1070: Could not resolve com.foo.bar.pig.IsInternal using imports: [, org.apache.pig.builtin, org.apache.pig.impl.builtin.]
The Java code itself is fine (I've tested it), and I've tried playing around with different class paths in the DEFINE without any luck. I haven't found anything of help online. How would I fix this?
I'm a little queasy about the packaging into a .jar by hand. Can you try putting into an eclipse project and exporting from there?
Also, can you confirm the directory structure of the jar? It should be like this: PiggyBank.jar/com/foo/bar/pig/IsInternal.class
This error look like you not fix the class path path error for your UDF i was also have the same problem you in this link i explain how to fix above error . Hope it will help you .

Exception in thread "main" java.lang.NoClassDefFoundError

package pack;
public class sample{
public static void main(String input[])
{
NumberFormat numberFormat = new DecimalFormat("#,##0.00##");
System.out.println(numberFormat.format(44533125.00));
}
}
the code is working fine in the current dir.. (c:/myprogram/).
after that i copy the sample.class file and paste it in other dir(d:/myprogram).
i got error while running, like
Exception in thread "main" java.lang.NoClassDefFoundError: sample (wrong name: pack/sample)
In java .class file can run anywhere right? but why i am not able to run?
You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run
java pack.sample
(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)
If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:
javac -d classes Sample.java
or
javac -d classes src/pack/Sample.java
will (in both cases) create
classes/pack/Sample.class
You could then run
java -cp classes pack.Sample
IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it.
The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.
If you are not using a single java/class file you can also remove the package statement.

Categories