Error: Could not find or load main class - java

I am trying to run a java file in the cmd prompt in Windows 7.
I get the error:
Error: Could not find or load main class
I actually just save a new simple file to check if there were problems with the package inside eclipse; this new file is saved just as:
C:\Users\User5\Documents\eclipse\test\Example.java
class Example {
// A Java program begins with a call to main().
public static void main(String args[]) {
System.out.println("Test.");
}
}
I changed the classpath for lucene's jar's recently, and I am not really sure if this is the problem.
There are many other threads about this issue, such as:
Could not find or load main class
but, there seem to be other concerns that solved their issues.
In this case, I have saved just a plain file in notepad, and while I can get the file to compile, and it seems to create the class file, it is still spitting this error back.
This is the dir, which seems to show that the class is there:
C:\Users\User5\Documents\eclipse\test>dir
Volume in drive C has no label.
Volume Serial Number is 3E0D-3B82
Directory of C:\Users\User5\Documents\eclipse\test
12/07/2015 10:15 AM <DIR> .
12/07/2015 10:15 AM <DIR> ..
12/07/2015 10:04 AM 301 .classpath
12/07/2015 10:04 AM 380 .project
12/07/2015 10:04 AM <DIR> .settings
12/07/2015 10:05 AM <DIR> bin
12/07/2015 10:51 AM 428 Example.class
12/07/2015 10:15 AM 162 Example.java
12/07/2015 10:05 AM <DIR> src
4 File(s) 1,271 bytes
5 Dir(s) 10,000,461,824 bytes free
C:\Users\User5\Documents\eclipse\test>java Example.java
Error: Could not find or load main class Example.java

This should to the trick - don't append .java
C:\Users\User5\Documents\eclipse\test>java Example

I think I need to make this in a larger post:
C:\Users\User5\Documents\java\test>java Example
Error: Could not find or load main class Example
I just accidentally posted the wrong copy to the original post here, but the file is not running as just a call to the file. This is not the only file I cannot get running; nothing will run in the cmd prompt, although everthing runs fine in eclipse.
Thank you'all for your help, I really appreciate you!!

C:\Users\User5\Documents\eclipse\test>java Example.java
will not run - take out the .java part and simply run
C:\Users\User5\Documents\eclipse\test>java Example
To make sure you do not have an issue with your CLASSPATH variable, do
set CLASSPATH=
java Example
If that is the issue, your classpath variable gets corrupted, you can go to the Control Panel, somewhere in the advanced section to update environment variables.

I got the same error when I needed to take inputs(arguements) at runtime using terminal/Command prompt and corrected it it by compiling and running the java class as below:
javac packageName/Example.java
java packageName/Example
NOTE: The reason for this error in my case was that the java class was created inside a package and that required compilation and execution in a different way as above mentioned.

Related

Java Class name with _ - NoClassDefFoundError with "wrong name" [duplicate]

I have a problem while trying executing my java application.
Whenever I try to execute the program through the command
java ProgAudioJ
I get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
at java.lang.ClassLoader.defineClass1(NativeMethod)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(NativeMethod)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ProgAudioJ. Program will exit.
If I remove from my code:
package es_2011;
Everything works perfectly. How do I solve the problem?
Because I found these answers unclear, here is what you need to do.
First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):
cd /myFolder/myPackage
javac MyClass.java
cd ..
java myPackage.MyClass
Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like
package com.somedomain.someproject;
you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):
cd /myCode
java com.somedomain.someproject.MyMainClass
Try using:
java es_2011.ProgAudioJ
(instead of java ProgAudioJ).
I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.
Try this (compile and run):
dir
2011-02-10 00:30 <DIR> .
2011-02-10 00:30 <DIR> ..
2011-02-10 00:27 58 es_2011
javac es_2011/ProgAudioJ
java es_2011.ProgAudioJ
It's quite clearly stated there:
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,
src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;
(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.
Try this,
Compile your class using below command
$ javac ProgAudioJ.java -d .
Run your application by command
$ java es_2011.ProgAudioJ
The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.
I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file.
It has been packaged up as
es_2011/ProgAudioJ
In order to run it - you have to either move up a directory:
If you are here: (Windows)
src\es_2011\
move to
src\
Then run the line:
java es_2011.ProgAudioJ
This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).

ANTLR Ubuntu Java Makefile

Ive been trying to test a simple project written by some students. Im testing it on Ubuntu and the project requires me to use ANTLR and a Makefile.
It has been a nightmare to find a configuration of makefile, files and folders that compiles and executes successfully.
So basically this is the folder/file setup:
Makefile
test.txt
laboratorio/
lab02/
Main.java
Lab02.g
The contents of Main.java are fairly simple. They just read the grammar token by token:
package laboratorios.lab02;
import org.antlr.v4.runtime.*;
public class Main{
public static void main(String[] args) throws Exception{
try{
Lab02 lexer = new Lab02(new ANTLRFileStream(args[0]));
while (lexer.nextToken().getType() != Token.EOF);
}catch(ArrayIndexOutOfBoundsException aiobe){
System.err.println("Must provide a valid path to the filename with the tokens");
System.exit(1);
}catch(Exception e){
System.err.println("Must provide a valid path to the filename with the tokens");
System.exit(1);
}
}
}
The contents of Lab02.g (the grammar) are irrelevant for the problem, but it produces a Lab02.java file (with package laboratorios.lab02) that must be compiled and referenced by the Main.java file.
The problem came when trying to test the makefile on Ubuntu. With every configuration I tried I kept getting errors (when compiling or running the Main java file) like:
java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream
Could not find or load main class org.antlr.v4.Tool
Error: Could not find or load main class
Ive followed the exact steps of this tutorial to set up antlr on my pc: Getting Started with ANTLR v4
After many unsuccessful attempts I arrived at this magical configuration that made the whole project work nicely.
Makefile:
make: gramatica main
gramatica: laboratorios/lab02/Lab02.g
java org.antlr.v4.Tool laboratorios/lab02/Lab02.g
main: laboratorios/lab02/Lab02.java laboratorios/lab02/Main.java
javac laboratorios/lab02/Lab02.java laboratorios/lab02/Main.java
run: laboratorios/lab02/Main.class
java -cp ${CLASSPATH}:Lab02.class laboratorios/lab02/Main test.txt
clean:
rm -rf laboratorios/lab02/*.tokens
rm -rf laboratorios/lab02/*.class
rm -rf laboratorios/lab02/Lab02.java
rm -rf laboratorios/lab02/*~
I required me to set the CLASSPATH in the bashrc like this:
export CLASSPATH=.:~/your/path/to/antlr/antlr-4.2-complete.jar
As you would find in the tutorial I previously mentioned (Getting Started with ANTLR v4), it says you should write the Classpath using quotes. COMPLETELY IGNORE THAT ADVICE! Whenever I used quotes the main class wouldnt run as ANTLR was 'missing'. Also, the aliases in the tutorial are completely optional.
Some other things worth pointing out are that placing the Makefile inside the laboratorio/lab02/ folder was not helping either. Moving it out and referencing the java files from the outside helped A LOT. And the characters ".:" helped to execute the Main class as it would say it couldn't been found.

Cannot find output .class files using javac?

I type javac helloworld.java at cmd in win 7.
C:\Users\User\Downloads\java_tut>javac HelloWorld.java
C:\Users\User\Downloads\java_tut>dir *.class
Directory of C:\Users\User\Downloads\java_tut
03/28/2014 05:42 PM 429 YourClassName.class
C:\Users\User\Downloads\java_tut>
I searched the following directories for helloworld.class:
java, jre, jdk, java_tut, jre/bin, jdk/bin, and my entire harddrive.
I did need to manually add the /jdk/bin directory to my path. I wonder if that matters?
Another possible reason is an empty .java source file.
This causes javac to silently produce nothing.
I experienced that effect with a Hello Word program and a Macintosh editor - which would save with Cmd-S, but does not save with Ctrl-S. I learned this after 20 years of Java programming.
If HelloWorld.java compiled without any errors, then the file HelloWorld.class should definitely be in the java_tut directory.
EDIT (based on your comments and edits):
Check if your Java source file HelloWorld.java looks as follows:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hellow, World!");
}
}
The class must have the same name as the Java source file or you get following compiler error message:
[515]% javac HelloWorld.java
HelloWorld.java:1: error: class YourClassName is public, should be declared in a file named YourClassName.java
public class YourClassName {
^
1 error
Although I asked about the package declaration, I can tell you the correct approach:
Let's assume you have a Java class with that source:
package my.test;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
Assuming your project root directory is
C:\Projects\java_tut
you must put the source file HelloWorld.java into the directory
C:\Projects\java_tut\my\test
Afterwards you compile and start this little program while being in the java_tut directory with the following commands:
C:\Projects\java_tut> javac my/test/HelloWorld.java
C:\Projects\java_tut> dir my\test
[...]
28.03.2014 09:35 <DIR> .
28.03.2014 09:35 <DIR> ..
28.03.2014 09:35 434 HelloWorld.class
28.03.2014 09:34 134 HelloWorld.java
[...]
C:\Projects\java_tut> java my.test.HelloWorld
Hello World.
Explanation: If working with packages (and you always should use packages for your classes) you must not "sit" in that package, but always run the commands from outside the package (folder).
YourClassName.class is the correct file in this case. The class name isn't generated based on the .java file's name. It's generated based on the class name inside the .java file. In my .java file, I named the class YourClassName and not HelloWorld.

Trying to install malabar-mode in emacs, getting "Cannot open load file, malabar-mode"

So I've followed the instructions on the malabar-mode github page.
I have emacs packages set up, with melpa added as a an archive (which is where malabar-mode is). mvm's containing directory is in my exec-path, and I have added the following to my ~/.emacs file, as per the README's instructions:
(setq semantic-default-submodes '(global-semantic-idle-scheduler-mode
global-semanticdb-minor-mode
global-semantic-idle-summary-mode
global-semantic-mru-bookmark-mode))
(semantic-mode 1)
(require 'malabar-mode)
(setq malabar-groovy-lib-dir "/path/to/malabar/lib")
(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode))
However, when I start up emacs, I get:
Warning (initialization): An error occurred while loading `/Users/kalaracey/.emacs':
File error: Cannot open load file, malabar-mode
How can I get malabar mode to work? I am using Emacs 24, which has CEDET built in, so that is why I have added the above code to my ~/.emacs file (as per the instructions).
malabar-mode was added to MELPA so you do not need to install it by hand anymore.
To install
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(package-install 'malabar-mode)
(package-install 'flycheck) ;; This is optional but nice to have
In .emacs.
Because malabar-mode takes so long to load (over 30 seconds on my box), I have it delay loading until I try and find a .java file.
(defun malabar-mode-bootstrap ()
(require 'cedet)
(require 'semantic)
(load "semantic/loaddefs.el")
(semantic-mode 1);;
(require 'malabar-mode)
(load "malabar-flycheck")
(malabar-mode)
(flycheck-mode))
(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode-bootstrap))
Also, take a look at https://github.com/m0smith/maven-pom-mode for editing the pom.xml.

Magic value error when I run my applet

I create an Applet and I generate the jar file with the following code
JAR FILE
"c:\arquivos de programas\java\jdk1.7.0_05\bin\jar" cvf C:\Users\lucas\Desktop\AbrirAplicativo3000.jar C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\WebcamApplet.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\QRCodeProcessor.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\QRCodeListener.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\OpenCVWebCam.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\CVImageProcessor.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\AbstractProcessor.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\gui\ImagePanel.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\gui\LabelPanel.class C:\Users\lucas\workspace\WebcamApplet\bin\com\colorfulwolf\webcamapplet\gui\LoadingScreen.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\StringsResourceTranslator.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\client\j2se\BufferedImageLuminanceSource.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\client\j2se\CommandLineRunner.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\client\j2se\GUIRunner.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\client\j2se\ImageConverter.class C:\Users\lucas\workspace\WebcamApplet\bin\com\google\zxing\client\j2se\MatrixToImageWriter.class
and I singed the JAR file normally.
I put the JAR file in a visible HTTP (http://www.netimoveis.com/AbrirAplicativo3000.jar)
In my ASPX page, I'm calling the APPLET following this code
<applet code="com.colorfulwolf.webcamapplet.WebcamApplet"
archive="http://www.netimoveis.com/AbrirAplicativo3000.jar, http://www.netimoveis.com/AbrirAplicativoAssinado3000.jar"
height="550" width="550">
</applet>
But when I try to run, I got the error
Incompatible magic value 218774561 error in applet
Someone can help me ?
Your AbrirAplicativo3000.jar is not correctly packaged. If you look inside, it has this structure:
META-INF/
C:/
Users/
lucas/
workspace/
WebcamApplet/
bin/
com/ --> this is where the jar should start from.
...
Try using the -C option on the jar command like this:
"c:\arquivos de programas\java\jdk1.7.0_05\bin\jar" cvf C:\Users\lucas\Desktop\AbrirAplicativo3000.jar -C C:\Users\lucas\workspace\WebcamApplet\bin\ .
Also it's not the first time that this magic number comes up on SO, although it seems more related with a bad URL. However I did download the jar with your supplied URL so just try re-packaging it.
The magic value error means that the class file doesn't start with the integer 0xCAFEBABE as it should. You probably had a transfer or compression problem.
If you can open the file in an hex editor, you may look for those bytes.

Categories