(Java) "error: cannot find symbol" when compiling? - java

This is the first time I'm coding in Linux, I'm used with writing in Windows. So my problem: I have a map called Train, in there I have two classes: Train.java and Lab1.java.
When I try to compile(using javac Train.java in the terminal), I get no errors with one of the class, but with the other one I get this error:
Lab1.java:58: error: cannot find symbol
Train trainThread2 = new Train(tsi, trackSemas, secondTSpeed, 2, true, tsispeed,8);
This code is from the Lab1.java class. It points at the Train and shows this error above, that it cannot find the symbol Train
I didn't find any information that helped me, so I'll try asking here.

Lab1 depends on the class Train so you need to tell the path of the compiled Train class in its classpath. The easiest solution is to compile both at once:
javac Train.java Lab1.java

Related

Java cannot find symbol when I do a Maven Build, but code works in debugger

I am working on an AEM site and I am trying to get the page property teaserImage I have the follow in my Java class, but when I do a build I get a cannot find symbol error.
public String getTeaserImg() {
return this.getPageManager().getPage(url).getProperties().get("teaserImage").value;
}
But if I put a breakpoint on this similar function
public Page getPage() {
return this.getPageManager().getPage(url);
}
And then execute this.getPageManager().getPage(url).getProperties().get("teaserImage").value in an Evulate expression window it gives me the property, so I'm not sure why on build it doesnt work when it works in this scenario.
Cannot find symbol error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
In your example you are trying to get a property. If you can get that when you are debugging the code, that maybe means you forgot to add that property when you are trying to build the application.

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

java.lang.LinkageError when loading class from .zip file

I'm trying to make a class loader that will load .class files inside a .zip file. I followed this tutorial (https://weblogs.java.net/blog/2008/07/25/how-load-classes-jar-or-zip), but when i run the program, i get this error:
java.lang.LinkageError: loader (instance of org/freeforums/geforce/genforcer/main/ZipClassLoader): attempted duplicate class definition for name: "test/TestClass"
The error comes from line 30, which is:
return defineClass(filename, out.toByteArray, 0, out.size());
I call the method by using:
zipClassLoaderObj.findClass("test.TestClass");
Does anyone know what i'm doing wrong?
Found the problem. I was calling the method two times because it was in a 'for' loop with a bunch of other code. Just took it out of the loop, and now it works perfectly. Don't know why i couldn't figure that out myself. Thanks for the help!

error while running java files

I have two files like read.java and sufer_type.java. The surfer_type.java needs some methods which are present in a .jar file. Before executing surfer_type.java I need to run read.java and use the data from read.java. So I used the statement:
read r=new read();
in surfer_type.java and I am compiling surfer_type.java like:
javac -classpath netcdfAll.jar surfer_type.java:
But I am getting an error:
surfer_netcdf.java:30: cannot find symbol
symbol : class read
location: class surfer_netcdf
read r = new read();
^
surfer_netcdf.java:30: cannot find symbol
symbol : class read
location: class surfer_netcdf
read r = new read();
You are trying to instantiate a class read, which it is telling you does not exist (or rather, it cannot find).
Make sure the class exists and is properly imported in the class where you are trying to use it.

"cannot find symbol" on a wsdl Java Client

This is part of a lab exercise for a course I'm doing, it's not assessable, just a learning exercise. Not sure why but the tut didn't go through it, so I just went through it at home but I'm stuck on the last part.
I'm trying to write a java WSDL client to access http://www.nanonull.com/TimeService/TimeService.asmx?WSDL - I should input UTC+10 to display the current time. Below is the code that I have written:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.GetTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
When I try and compile the code I get the following error:
C:\Program Files\Java\jdk1.6.0_22\bin>javac -d . "c:\Program Files\Java\jdk1.6.0
_22\bin\time\Client.java"
c:\Program Files\Java\jdk1.6.0_22\bin\time\Client.java:13: cannot find symbol
symbol : method GetTimeZoneTimeResponse(java.lang.String)
location: interface time.TimeServiceSoap
String result = port.GetTimeZoneTime("UTC+10");
^
1 error
Any thoughts on what I'm doing wrong?
Did you mean
String result = port.getTimeZoneTime("UTC+10");
with a lowercase g? Java method names are case-sensitive, so it won't recognize the method if you get its letter casing wrong. As per both WSDL's TimeServiceSoap documentation and Java naming conventions, method names are in camel case beginning with a lowercase letter.
What does your TimeServiceSoap look like?
Perhaps you meant to use getTimeZoneTime() (starting with a lower case letter)?

Categories

Resources