Java: Oracle example code not running: "NoClassDefFoundError" - java

I'm trying to learn how to set up a GUI layout, so I'm reading through this page, which has this example code.
The problem is, when I run the code (either through Geany or the command line), it seems to compile correctly, then it gives me this error when I try to run it:
Since this is a sample from Oracle, I am working under the assumption that I am not running the program correctly, but I don't know what I'm doing wrong. Does anyone have any idea what I could be doing wrong?

GridBagLayoutDemo.java is declared to be in the package layout, but you placed it in the default package (no subdirectory). So either remove the package statement, or move the file in a directory called layout and then call javac layout/GridBagLayoutDemo.java && java layout.GridBagLayoutDemo.

Related

VS Code: The declared package "controller" does not match the expected package ""

So, I have checked some other threads that are about this topic, such as the following:
Eclipse says package declaration does not match expected package ""
The declared package test does not match the expected package
libgdx: The declared package does not match the expected package ""
But they all deal with Eclipse (or Eclipse + Maven), and I am just using VS Code without all the fancy capabilities of an IDE or a package manager.
As the title suggests, I am getting the message:
[Java] The declared package "controller" does not match the expected package ""
I get this message for every single file that I attempt to give a package using the syntax:
package myPackageName;
even if the file in question is defiantly in the right folder. This problem only occurs as a visual, i.e. everything compiles fine using javac. I have been dealing with it ever since I started using VS Code for my Java Projects.
How do I make the red squigglies and the red lettering in the file explore tab go away in the case of perceived package errors?? It drives me nutty, I can't quickly find the files that actually have errors in my project because everything is marked red except the Driver.
Here is a picture: picture
[edit] Got rid of the unrelated compile time errors So you can see it compiles.
All classes in the package should be open on VSCode (on different tabs).
Same thing happens when you want to use other public classes in same directory: it should be visible but when you run the program, you will get a compile-time error. Then again the solution is to have all classes open on different tabs.
Hope it helps
A bit late to it, but here is my finding:
Your parent folder is proto9, so in all your child folders, i.e. controller, model and view. You should write package proto9.controller, package proto9.model etc. You can compile the .java file just fine in the correct directory, but not 100% sure why we cannot run it while in the directory of the file is at, java.lang.NoClassDefFoundError is returned when attempted so.
To successfully run it, in the terminal (assuming your files are compiled):
# cd one level out of your parent directory
PS C:\Users\Issac\Dropbox\Apps\Celerious Text\proto9> cd ..
PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.controller.MainController
...
PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.view.MainView
etc.
Hope this works for you as well!!

Why do I need to delete the package line to run Java outside of NetBeans?

So I'm completely new to programming, and I've been writing some Java with the NetBeans IDE. My code runs fine within NetBeans, but I've tried to run it using the command line as well. However, if I run it from the command line, I have to delete the line:
package firstprogram;
which NetBeans automatically places at the top of each new file, or I get the error:
Error: Could not find or load main class FirstProgram
However, if I do delete the line, then the program no longer runs within NetBeans! It doesn't seem right that I have to choose whether to run a .java from within NetBeans or without.
The research I've done makes me think that this is something to do with directory structure? But everything I read on that goes straight over my head. NetBeans has a structure with "build", "dist", "nbproject", and "src", but when I use the command line I just place the .java file in an empty directory and javac from there.
Any explanation is appreciated! The books and tutorials I'm learning from either assume you're just using NetBeans or don't have the package line at all.
You can compile your class using javac command from anywhere, as long as you provide correct relative or absolute path. The problems come when you want to run your program using the java program.
You have to provide the correct path corresponding to your package declaration. For example, if I had 'MyClass' in package mypackage, first line would look like this:
package mypackage;
class source stored on disk:
c:/MyNetbeansProject/src/mypackage/MyClass.java
Compiled bytecode:
c:/MyNetbeansProject/build/classes/mypackage/MyClass.class
Now, if I would have opened a command prompt/terminal in folder c:/MyNetbeansProject/build/classes/, I could run the program using java mypackage/MyClass or java mypackage.MyClass.
However, if i would be somewhere else, I would have to say where the class files are located using the cp option: java -cp c:/MyNetbeansProject/build/classes mypackage/MyClass. The path in cp option can be relative or absolute, use "" when it contains spaces.
Package are directory architecture.
If your class is in the package com.acme.test, the class should be in the com/acme/test directory.
Instead of placing your class in an empty folder, place it in a folder named firstprogram and do javac firstprogram/youclass.java
The package (and folder) permit you to arrange your architecture with logical pattern.
More info here : http://www.tutorialspoint.com/java/java_packages.htm
So like OcterA said, you should keep organized, but with only one class this is not the issue. I believe that your problem is that you are not entering the correct command into the command line.
First cd to the correct directory and when you want to execute a file within a package in that directory you need to enter
java packageName.className
In this case
java firstprogram.FirstProgram

Searching image for main class?

I'm just starting to use swing in java, and I've come across a weird problem. I'm trying to add a splash screen (http://wiki.netbeans.org/Splash_Screen_Beginner_Tutorial), and everything works fine until I add splash:src/Splash.png to my VM options. Once I do that, I get this error:
Error: Could not find or load main class splash:src.splash.png
Java Result: 1
It's as if java is trying to find my main class inside the png file??? I believe my file is in the correct place, this is what it looks like:
And my main.java is just a default swing frame form. Any clue whats going on?
Thanks!
Edit: New file layout:
Changed manifest and VM settings, same error: Error: Could not find or load main class splash:images.splash.png
Java Result: 1
From the error you've listed, it looks like you've missed the - from the splash vm option. This means the command line will be:
java splash:src.splash.png Main.class
Means the Java executable thinks you're asking it to run splash:src.splash.png.
The command line should be (note, you don't need to include the src directory):
java -splash:images/splash.png Main.class
Check your VM Options in Netbeans and correct it to -splash:images/splash.png.
I strongly suggest you puting files other than .java (source files) in another directory. In the most common case src is where Java IDEs (at least Eclipse and NetBeans) put the source files, it is pretty much standard, and the hierarchy of the folder inside it has to match your Java packages names. So, move the image to another folder, let's say img and try again.

Having trouble compiling a Java program, I am new to it. Can't figure out what to do with directories

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.

Calling Java from RPG, ClassNotFoundError for all user except me

I would highly appreciate your help with a ClassNotFoundError while calling a Java method from RPG. This happens when any other user, except me, tries to run my program.
It literally seems a problem with CLASSPATH or access to jar files but I tried from that aspect too.
Explanation:
I have an RPG program which calls a java method in a jar file. My CLASSPATH has the right jars included (JtOpen jar files). It works perfectly when I run the RPG program from a Green and Black screen (Iseries session manager). Where as it throws the following error message whenever another user tries to run it the same way:
RPG Procedure received Java Exception java.lang.NoClassDefFoundError: com.ibm.as400.access.AS400 when calling method "invokeApiPgm" with signature "([Ljava.lang.String;Ljava.lang.String;I[Ljava.lang.String;)Z" in class "callIceApi.CallIceApiPgm".
The signature above is also the same as that of the method i call. Does the class the Java is complaining seem to be that of AS400 or could it be mine? I can see that the AS400 class is available as public. The class object was created successfully by the constructor of my class. It just fails when calling the main method invokeApiPgm.
What do you think I am missing or messing up with, guys?
Thanks for your help!
Tewelle
How about a CL program that explicitly sets the classpath and then calls your RPG program?
The CLP:
PGM
ADDENVVAR ENVVAR('CLASSPATH') VALUE('.:/dir1:/dirA/dirB:/javaToolkit/jt400.jar') LEVEL(*JOB)
CALL PGM(MYLIB/MYPGM)
ENDPGM
This page may also be helpful.
In the beginning I had all Jars and files in the same outer Jar which I exported from Eclipse and adding the Jar's path to my CLASSPATH couldn't work for me. However, I don't know why though, it worked after taking the JT400.Jar separately out and then including its path in the CLASSPATH, as Benny's answer, with another separate inclusion to the main project jar worked right.
Thanks Benny!

Categories

Resources