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.
Related
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.
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!
When i try to create web service proxy in java i always get. I look to web service but it works, i can invoke it. Why i can not create proxy?
java.lang.NoClassDefFoundError:
xxx/GetBibliografijeXml
(wrong name:
xxx/GetBibliografijeXML)
at
java.lang.ClassLoader.defineClass1(Native
Method) at
java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at
java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at
java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at
java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at
java.security.AccessController.doPrivileged(Native
Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.SchemaAnalyzer.getValueClassBeanInfo(SchemaAnalyzer.java:465)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.ComplexTypeBindingModeler.structuredType(ComplexTypeBindingModeler.java:142)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.ComplexTypeBindingModeler.complexType(ComplexTypeBindingModeler.java:442)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.LiteralSchemaTypeModeler.complexType(LiteralSchemaTypeModeler.java:495)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.LiteralSchemaTypeModeler.schemaType(LiteralSchemaTypeModeler.java:373)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.LiteralSchemaTypeModeler.globalElement(LiteralSchemaTypeModeler.java:446)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.SchemaAnalyzer.schemaElementTypeToLiteralType(SchemaAnalyzer.java:403)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.OperationModeler.getElementTypeToLiteralType(OperationModeler.java:588)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.DocLiteralOperationModeler.buildInput(DocLiteralOperationModeler.java:527)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.DocLiteralOperationModeler.buildOperation(DocLiteralOperationModeler.java:256)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.OperationModeler.process(OperationModeler.java:93)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.processSOAPOperation(WSDLModeler.java:1086)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.processBindingOperation(WSDLModeler.java:1020)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.createNewPort(WSDLModeler.java:884)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.processPort(WSDLModeler.java:757)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.processService(WSDLModeler.java:671)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.internalBuildModel(WSDLModeler.java:396)
at
oracle.j2ee.ws.common.processor.modeler.wsdl.WSDLModeler.buildModel(WSDLModeler.java:215)
at
oracle.j2ee.ws.common.processor.config.ModelInfo.buildModel(ModelInfo.java:173)
at
oracle.j2ee.ws.common.processor.Processor.runModeler(Processor.java:72)
at
oracle.j2ee.ws.tools.wsa.AssemblerTool.run(AssemblerTool.java:95)
at
oracle.j2ee.ws.tools.wsa.WsdlToJavaTool.createProxy(WsdlToJavaTool.java:356)
at
oracle.j2ee.ws.tools.wsa.Util.createProxy(Util.java:838)
at
oracle.jdeveloper.webservices.model.proxy.ProxyGenerator.doGeneration(ProxyGenerator.java:553)
at
oracle.jdeveloper.webservices.model.proxy.ProxyGenerator.generateImpl(ProxyGenerator.java:365)
at
oracle.jdeveloper.webservices.model.proxy.ProxyGenerator.mav$generateImpl(ProxyGenerator.java:77)
at
oracle.jdeveloper.webservices.model.proxy.ProxyGenerator$1ThrowingRunnable.run(ProxyGenerator.java:206)
at
oracle.jdeveloper.webservices.model.GeneratorUI$GeneratorAction.run(GeneratorUI.java:446)
at
oracle.ide.dialogs.ProgressBar.run(ProgressBar.java:551)
at
java.lang.Thread.run(Thread.java:595)
Well, it looks like something is using the wrong case:
java.lang.NoClassDefFoundError: xxx/GetBibliografijeXml
(wrong name: xxx/GetBibliografijeXML)
Note the casing of "XML".
Check everywhere that the name is used, and make sure the case is consistent everywhere. Is the class an autogenerated one, or one you've written yourself?
Also bear in mind that if you're on a case-insensitive operating system and the classes are being loaded straight from disk (without coming from a jar file etc), it could be finding an old class file - you won't be able to have two classes whose names differ only in case on such a file system, as the class files will clash. If your WSDL uses both cases, it may have generated two classes, but then you only end up with one file on disk :(