I have recently been getting this Java compile error every time I try to compile code that creates an instance of a class that I have created. I've tried compiling manually, compiling from a different location, and even tried compiling in safe mode. I have also completely reinstalled Java on my computer. Here's an example of code I write and the error I always get:
Instance creator class:
public class Nothing {
public static void main(String args[]) {
Can World = new Can();
}
}
Instantiated class:
public class Can {
public Can() {
System.out.println("Test");
}
}
The compile error:
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
2 errors
Someone who knows Java better than me has tried to compile files that I have had problems with with no success. Also, when I run the code from within Eclipse, my IDE, it runs like it should.
Any suggestions at all or solutions would really, really be appreciated. I would really hate something like this to be the thing that prevents me from programming. Thanks again.
EDIT: I used to be able to compile the classes like this, until only recently I started receiving this error. I am compiling using an external tool I have created in the Eclipse IDE, but I have also tried compiling simply by navigating to the directory the two files are in in a CMD window and used javac Nothing.java yet the same error arises. I have also tried compiling Can.java first (which compiles), and then Nothing.java, but this fails as well. Here is the text that I get when compiling from a CMD window:
02/09/2011 06:44 PM <DIR> .
02/09/2011 06:44 PM <DIR> ..
02/09/2011 03:45 PM 289 .classpath
02/09/2011 03:45 PM 382 .project
02/09/2011 03:45 PM <DIR> .settings
02/09/2011 06:00 PM 75 Can.java
02/09/2011 05:49 PM 102 Nothing.java
4 File(s) 848 bytes
3 Dir(s) 64,669,216,768 bytes free
C:\Users\Alex\Mindstorms\NXT\leJOS NXJ\Moment>javac Nothing.java
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
Nothing.java:4: cannot find symbol
symbol : class Can
location: class Nothing
Can World = new Can();
^
2 errors
The problem is that you need to have an appropriate import statement (if in separate packages), and you need to run the Java compiler from the appropriate directory. Supposing your directory structure looks like:
src/
com/
yourdomain/
example/
Can.java
Nothing.java
Then you would need the following at the top of both your *.java files:
package com.yourdomain.example;
And you should put the following import statement in Nothing.java (technically this is not necessary when they are both in the same package, but is necessary when in separate packages, and it's a good habit):
import com.yourdomain.example.Can;
And then you would invoke the compiler from within the src directory as follows:
javac com/yourdomain/example/*.java
And you could then run this program using:
java com.yourdomain.example.Nothing
By the way, you really shoudn't be building projects by hand this way; you should use an automatic build system such as Maven or Ant. If you create your project using the NetBeans IDE, aside from giving you simple "Build", "Run", and "Build & Run" buttons plus all sorts of nice IDE features (code highlighting, incremental compilation and suggestions for fixes), it will generate an Ant build project for you.
Edit
Based on your updated question,... note that Eclipse's compiler is distinct from javac. If you installed javac using Cygwin or if you've been sharing your files between Windows and UNIX (possibly through a version control system), you may have run into an encoding issue. I would suggest resaving those files in UTF-8 and running unix2dos (or vice-versa if you installed javac via Cygwin) and recompiling. If that doesn't work, it might be worth reinstalling javac. Failing that, there's always Ubuntu ;).
By putting the code you've presented for each class into separate files (named Can.java and Nothing.java, of course) and having both files in the same directory they compile for me using the command line compiler:
javac Nothing.java
The error indicates that the compiler can't find the Can class when compiling Nothing. Do you have both files in the same directory?
I have figured it out. As it turns out, I had tried setting up an older version of Lego Robotics with Java on my computer, and I had created some environment variables to go with it. One of them was the CLASSPATH variable. I am pretty confident that Java was trying to read that variable (which I was no longer using) to look for my classes. Once I deleted it (and the other old variables that I wasn't using), restarted my computer, and reinstalled the JDK, everything works.
Thanks for the help from everybody though :)
Related
The below error compelled me to dig into the build process of a cap file in the command line without using the IDE. So now, I can build a cap file from the command line using java/javac series of commands. But I have this one applet which successfully created a cap file if built via eclipse IDE but I am encountering an error when i try to build in the command line. I am also having same error when I tried in a correctly setup gradle build settings/environment. This is the error:
[ant:convert] [ INFO: ] Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
[ant:convert]
[ant:convert]
[ant:convert] warning: You did not supply export file for the previous minor version of the package
[ant:convert] [ INFO: ] conversion completed with 1 errors and 1 warnings.
[ant:convert] error: Class org/dx/tools/TestApplet, specified in -applet option, is abstract.
Take note, this is a working and tested applet in combination with other applets that uses this one. And this builds in the eclipse IDE.
Also I am able to generate the .class file. The problem is during the convertion of the class file to cap file.
Here is how it look like:
package org.dx.tools;
import org.globalplatform.GPSystem;
import org.globalplatform.SecureChannel;
import javacardx.apdu.ExtendedLength;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
import javacard.framework.APDU;
import javacard.framework.APDUException;
import javacard.framework.Applet;
import javacard.framework.AppletEvent;
public abstract class TestApplet extends Applet implements AppletEvent,
ExtendedLength {
...
}
This .cap is one among five others. The others are not abstract, but inherits from this one. Since it build in eclipse IDE, I can actually build the other cap files by taking the output of the IDE had produce. First, I jar the classes since as I said I can create the classes and feed to java conversion command to build the other caps, and I also use the TestApplet.exp that the IDE generated.
UPDATES: 2019/11/17
Here is the actual java command options that is able to build from .class to .cap. I took this as exactly from the Eclipse IDE conversion log.
If an answer can confirm this is a current gradle limitation then I will accept this answer. If answer can show what is the correct gradle settings to make it work then I will accept this answer. Thanks.
This is very likely due to linking to a newer or older version of a library, while targeting a different Java Card version during your build process. Note that the converter creates pre-linked code, so it expects to link against the correct versions.
This is different from Java SE, where the classes can be linked at runtime and the location of methods and fields doesn't matter, as they are created during runtime.
I've resolved the issue by removing the applet {} block in build.gradle. As the particular cap being built is an abstract class, it does not have an applet aid.
I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed
I'm new in Android SDK. I'm using Emacs as IDE (I know about Android Studio). I created a project and I compiled it from a script named gradlew (It was created automatically when the project was created). APK is created successfully. Now, I'm trying to implement (with FlyMake) syntax error checker. The command used to do this is the following:
javac "main.java"
where main.java is the main of the application (there is only one file).
Obviously, javac doesn't know where is the SDK (API level 20). So I tell it as follow:
javac -sourcepath "~/opt/android/sources/android-20" "main.java"
but it throws a lot of errors like "class not found". For example:
/home/carlos/opt/android/sources/android-20/android/app/Activity.java:29: error: cannot find symbol
import android.content.IIntentSender;
^
symbol: class IIntentSender
location: package android.content
When I see the content of android/content, there is not a .java named IIntentSender
So, what's happening? Thanks for reading and answering!
This question was answered by CommonsWare:
The last time I used javac by hand, I used -classpath, not -sourcepath. The appropriate JAR for -classpath would be ~/opt/android/platforms/android-20/android.jar, assuming that ~/opt/android/ is where your Android SDK is installed. IIntentSender is generated Java source code (from AIDL) and will not be in Java form in the SDK sources directory. So, if FlyMake requires -sourcepath, I expect that you're going to be in a world of hurt.
Thanks!
I'm having a bit of trouble with compiling a Java RMI assignment in packages with the command line interface on Windows 10 (can't use Eclipse or any other IDE). I need to compile and run two .java files - RemoteBankServer.java, and RemoteBankClient.java. Both of these files use other .java classes (Bank.java, RemoteBankImpl.java, RemoteBank.java, Account.java and subclasses of Account.java).
Previously, with all of my files in one folder, I could easily compile and run my server and client with two command line windows, with:
javac RemoteBankServer.java
java RemoteBankServer
and the same for RemoteBankClient.
Now that I've tested that my code works fine, I need to organize them into java packages, following this structure:
- package name: edu.btp400.w2017.client
class name: RemoteBankClient
- package name: edu.bt400.w2017.server
class names: RemoteBankImpl, RemoteBankServer
- package name: edu.btp400.w2017.common
class name: RemoteBank (and all other classes, like Bank, Account, etc.)
I've created those folders (edu/btp400/w2017/) and have placed the appropriate classes in each folder, but I can't get it to compile. For now, I have these two lines at the top of each .java file:
package edu.btp400.w2017.server; //or .common or .client
import edu.btp400.w2017.*;
But I'm still getting compilation errors when trying to compile RemoteBankServer.java (in the server folder), such as this (which is weird, because RemoteBankImpl is in the same folder and RemoteBankServer:
RemoteBankServer.java:40: error: cannot find symbol
RemoteBankImpl b1 = new RemoteBankImpl();
^
symbol: class RemoteBankImpl
location: class RemoteBankServer
as well as similar errors saying that Account and other classes cannot be found. I've looked at various posts on how to compile with Java packages using the command line interface, but I haven't gotten it working. Any help would be greatly appreciated. Sorry for the long post.
Edit: this is the command I used to compile above (after storing the files in various packages):
I tried this in the folder containing edu, and got 27 compilation errors:
javac -cp . edu/btp400/w2017/server/RemoteBankServer.java
and this inside the server folder, and got 15 compilation errors:
javac RemoteBankServer.java
Edit: Thanks to user EJP, I found out that I needed to include the subfolders in my import statements: instead of just import edu.btp400.w2017.*; I needed to do:
import edu.btp400.w2017.common.*;
import edu.btp400.w2017.server.*;
import edu.btp400.w2017.client.*;
import edu.btp400.w2017.*;
That doesn't import all the classes in the subpackages indicated by *. The asterisk only refers to classes.
You need to import all the packages individually.
I've just created a project on Eclipse and imported some source files (existing project). But I can't compile it ! Ok, the project has got several source files, so I wanted to compile only the Main.java file (with eclipse not in the command line, in the command line it worked!) but all what I get is this error :
http://www.screencast.com/users/Amokrane/folders/Jing/media/82d772dd-10cd-4552-b1d0-3cf18bf39f13
As you can see the Main.java file is straighforward, just a hello world !
What's the matter ?
Thanks
"Unresolved compilation problem" means that the class hasn't compiled successfully. Eclipse will still let you run code that doesn't compile, but any of the specific bits which don't compile will throw this error. Look in the "Problems" tab to see what's wrong.
From the look of the Package Explorer view, every single class has a problem... perhaps the file location doesn't match the package declaration? That would match the position of the pink box just to the right of the vertical scrollbar for the class - it suggests that the error is right at the top of the file, which is where the package declaration would be.
You have a compilation error at the top of your Main.java file, just out of sight in the screenshot. Probably an unresolvable import or a wrong/missing package declaration.
It is simple in my case that the imported project needs 32 bit jre to run but cannot be compiled in the same. In IDE, if you click run, it will try to compile and run the project in single shot so fails due to 32 bit jre for compilation with the above reported error.
So i used 64 bit compiler and started to run and got compiled successfully but thrown error that needs 34 bit jre for some of the SWT used in the project. Again i changed the 32 bit jre and run the project, it is gone ! THE ERROR IS GONE NOW !
You can get the error "Exception in thread "main"
java.lang.Error: Unresolved compilation problem:" if your public class name differs from your file name.
example:
File Name:
ServiceRequest.java
Inside file, class is named differently; like
public class Service