I am working along with the ATM Case Study from Deitel java how to program 9th edition.
The case study is at chapter 13, page 546(in case someone has the book and would like to check),I am sure my code is 100% as the book suggested.
I have all the code set but when I try to run the program it is giving me this:
Error: Could not find or load main class come.example.atm.AtmRun
when I tried to compile the class by using terminal from the class path it gave me this error:
localhost:atm user$ javac AtmRun.java
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
2 errors
this is the class am running: straight forward but I cant seem to find the problem. any help?
package come.example.atm;
public class AtmRun {
public static void main (String[] args){
Atm theATM = new Atm();
theATM.run();
}
}
UPDATE: when i run the .class file from the bin directory of project using command java AtmRun i get this:
Exception in thread "main" java.lang.NoClassDefFoundError: AtmRun (wrong name: come/example/atm/AtmRun)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Class Atm has a constructor Atm() and public void run() along with other methods, the class is big so i think its better if I don't post the code it however you can check in the book if you can.
Note: I am using eclipse, other projects and classes work and run properly.
for me it worked. Please follow below steps:
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\Atm.java
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\AtmRun.java
Y:\HashmiAb\Desktop\Trash\test>java come.example.atm.AtmRun
Heloo
It matters how you use -d and -cp options of javac and java commands. I didn't use any of this options.
For more help please find the directory structure.
+test
-+come
-+example
-+atm
-AtmRun.java
-Atm.java
Thanks.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My application give me a error and I don't know how to solve!
I'm using Netbeans 8.0.
The error:
Exception in thread "main" java.lang.NoClassDefFoundError: autenticacao/Logon
at bolaofuleco.BolaoFuleco.main(BolaoFuleco.java:23)
Caused by: java.lang.ClassNotFoundException: autenticacao.Logon
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
Java Result: 1
Classes:
Principal:
package bolaofuleco;
import autenticacao.Logon;
import javax.swing.JFrame;
public class BolaoFuleco {
public static void main(String[] args) {
Logon login;
login = new Logon(new JFrame(), true);
}
}
And when I use the option clean and build of Netbeans appears this:
ant -f C:\\Users\\CaioCezar\\Documents\\NetBeansProjects\\BolaoFuleco -Dnb.internal.action.name=rebuild clean jar
init:
deps-clean:
Updating property file: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\built-clean.properties
Deleting directory C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build
clean:
init:
deps-jar:
Created dir: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build
Updating property file: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\built-jar.properties
Created dir: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\classes
Created dir: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\empty
Created dir: C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\generated-sources\ap-source-output
Compiling 17 source files to C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\build\classes
C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\src\autenticacao\Logon.java:207: error: cannot find symbol
if(Usuario != null){
symbol: variable Usuario
location: class Logon
1 error
C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
Your Logon class is not compiling,
C:\Users\CaioCezar\Documents\NetBeansProjects\BolaoFuleco\src\autenticacao\Logon.java:207:
error: cannot find symbol if(Usuario != null){ symbol: variable Usuario location: class Logon
Open "src\autenticacao\Logon.java" and you will need to fix the compiler error there (on line 207). Presumably, Usuario != null should be usuario != null for the if check (assuming it's a field, and following Java naming conventions).
In C:\ru\compscicenter\java\stacks> I have placed
AbstractStack.java
ArrayStack.java
LinkedStack.java
Stack.java
StackTest.java
The first string of each of these files is
package ru.compscicenter.java.stacks;
My my CLASSPATH examination:
C:\ru\compscicenter\java\stacks>echo %CLASSPATH%
.;C:\ru\compscicenter\java\stacks
When I'm in the stacks directory and try to compile StackTest, I fail to do that.
What I write and what I get is here:
C:\ru\compscicenter\java\stacks>javac StackTest.java
StackTest.java:4: error: cannot find symbol
public static void fill(Stack<String> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:12: error: cannot find symbol
public static <E> void dump(Stack<E> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
6 errors
Could you help me correct this?
Added later:
Then I did this:
C:\>javac c:\ru\compscicenter\java\stacks\*.java
Note: c:\ru\compscicenter\java\stacks\ArrayStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\>java StackTest
Exception in thread "main" java.lang.NoClassDefFoundError: StackTest (wrong name: ru/compscicenter/java/stacks/StackTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Could you help me again?
While compling use something like this
Javac -d . *.java
it will automatically create package structure and place class files in corresponding packages
The packages are interpreted as relative to the current directory. If you are specifying
package ru.compscicenter.java.stacks;
then the .java and .class files need to be in the directory ru\compscicenter\java\stacks\ from wherever you're running the commands. In this case, you need to run javac and java from C:\.
Start by defining a root directory for your app. Placing everything directly in c:\ is really not a good idea:
c:\myapp
then create a directory for the sources (.java files):
c:\myapp\src
and another one for your classes (.class files):
c:\myapp\classes
Now, your source tree should match exactly with your package tree, so you should have your sources files under
c:\myapp\src\ru\compscicenter\java\stacks
since they're all in the package ru.compscicenter.java.stacks.
Place yourself at the root of your app, in c:\myapp. The javac compiler expects files as arguments. And you want to place the compiled .class files in c:\myapp\classes. So you want to use
javac -d classes src\ru\compscicenter\java\stacks\*.java
This will create a folder tree matching exactly with the package tree in c:\myapp\classes, containing the compiled .class files. The root of the tree is c:\myapp\classes, so that's what you need to add to the classpath to run the app. And java expects a fully qualified class name:
java -cp c:\myapp\classes ru.compscicenter.java.stacks.StackTest
or, using a relative path, since you're in c:\myapp already:
java -cp classes ru.compscicenter.java.stacks.StackTest
I have this Java class:
public class SortAlgorithms {
public static void main(String... args) {
String out = "";
int vec1[] = readFile(args[0]);
out = out + write(vec1);
out = out + "=== INSERTION SORT ===\n";
insertionSort(vec1, vec1.length);
out = out + write(vec1) + "\n";
...
}
...
}
And I deploy it on other machine at my Boinc platform. There is a bash script to run this class:
#!/bin/sh
export JAVA_HOME="/usr/java/jdk1.6.0_34/"
export PATH=${JAVA_HOME}/bin:${PATH}
java SortAlgorithms 10 "output.txt" > saida.txt
And I receive this error:
<stderr_txt>
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1014198118 in class file SortAlgorithms
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
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(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: SortAlgorithms. Program will exit.
</stderr_txt>
Does anyone know what is happening?
Thnaks in advance.
Felipe
Your class file was not copied properly. It starts with string '<sof' which is definitely not a java bytecode start.
Looking at the Java API:
file is malformed or otherwise cannot be interpreted as a class file.
I believe that you need to compile your Java file on the machine which will be running the code.
While both maxkar and user1773630's answers seem right, it does sound like a .class file wasn't transferred properly. Compare checksums, or make a .jar?
If you've got jdk's with differing major versions installed, there can be class compatibility issues as well, which can cause a similar error.
I have wriitten following code using the Jena Library to print the URIs on a web page but it is showing a error. The code is
public static void test(String url)
{
try
{
System.out.println("to go");
Model read = ModelFactory.createDefaultModel().read(url);
System.out.println("to go");
StmtIterator si=read.listStatements();
System.out.println("to go");
while(si.hasNext())
{
Statement s=si.nextStatement();
Resource r=s.getSubject();
Property p=s.getPredicate();
RDFNode o=s.getObject();
System.out.println(r.getURI());
System.out.println(p.getURI());
System.out.println(o.asResource().getURI());
}
}
catch(JenaException | NoSuchElementException c)
{ }
}
Can anyone help me further with this problem???
It is not able to create a model read object. The URL is a web address of a xml page.
the following error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:39)
at com.hp.hpl.jena.JenaRuntime.<clinit>(JenaRuntime.java:37)
at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.<clinit>(RDFReaderFImpl.java:74)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.<clinit>(ModelCom.java:54)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:142)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:136)
at web.crawler.WebCrawler.test(WebCrawler.java:52)
at web.crawler.WebCrawler.main(WebCrawler.java:98)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
Add all the jars in the lib/ directory of the distribution. You can use the lib/* form for the classpath.
if you then still get missing org.w3c.dom.ElementTraversal (which is in xml-apis), it's because you have an older version of Xerces and old xml-apis.
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
the library slf4j*.jar is missing from your classpath.
Following are the libraries I had to add more:
import org.slf4j.*;
import org.apache.xerces.util.XMLChar;
import org.w3c.dom.ElementTraversal;
import org.apache.jena.iri.IRIFactory;
I solved it adding all the libraries availables at jena lib dir.
I gess Jena need another lib or jar file, but I dont know the especific .jar, so I add all of them.
Then it works!
I have built my own custom AWT classes in my home folder in java_src/classes.
Each of the java files contain the package classes; declaration at the top.
I also created a sample program called ScreenDemo.java and placed it in the java_src/ folder to use the custom AWT classes instead of java.awt.
//ScreenDemo.java
import classes.Screen;
class ScreenDemo
{
public static void main(String args[])
{
Screen.init(20,15,3);
}
}
But when i attempt to compile ScreenDemo.java,an error is displayed
java_src/ScreenDemo.java:1: package classes does not exist
import classes.Screen;
^
java_src/ScreenDemo.java:6: cannot find symbol
symbol : variable Screen
location: class ScreenDemo
Screen.init(20,15,3);
^
2 errors
When i add the path i encounter this error
Exception in thread "main" java.lang.NoClassDefFoundError: ScreenSample (wrong name: classes/ScreenSample)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
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)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: ScreenSample. Program will exit.
While compiling the code the compiler is complaining about the missing class Screen.java which i assume is inside "classes" package.
Either you need to add src\classes\ to the src folder list or move the Screen.java to the same location as ScreenDemo.java and then try to compile again.