Javac can't find class that is in the same directory - java

I am trying to compile a Java file and I'm getting this error message:
$ javac -cp "bc-j-mapi-w-2.4.jar;apache-commons/*;json-org/*;lib/*" BrightcoveVideoQueryPOI.java
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol : class BrightcoveAPI
location: class BrightcoveVideoQueryPOI
BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
^
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol : class BrightcoveAPI
location: class BrightcoveVideoQueryPOI
BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
^
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol : variable BrightcoveAPI
location: class BrightcoveVideoQueryPOI
BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
^
3 errors
This would suggest that javac cannot find the class BrightcoveAPI. I'm not sure what the problem is as it is in the same directory:
$ ls
apache-commons bc-j-mapi-w-2.4.jar BrightcoveAPI.class BrightcoveAPI.java BrightcoveVideoQueryPOI.java json-org lib

You need to include . (the current directory) in your classpath:
javac -cp ".;bc-j-mapi-w-2.4.jar;apache-commons/*;json-org/*;lib/*" BrightcoveVideoQueryPOI.java
Some notes:
. is in the default classpath, but if you use -cp to specify an explicit classpath, then it's only included if you specify it.
A previous version of this answer added . to the end of the classpath, but aioobe says that it's typically put first, which makes sense, so I've edited accordingly. (The classpath is searched in order, so if you have two copies of a class, one in . and one in a library, then you probably want the . version to supersede the library version, so you need to list it first. But of course, it's not usually a good thing to have two non-identical copies of a class!)
What you've pasted looks like a *nix shell, but you're using ;, which is the separator expected on Windows. (On *nix the expected separator is :.) This may well be correct, e.g. if you're using Cygwin, but I thought I'd mention it just in case.

If you do not search your current directory (your class path doesn't) javac won't add that directory in for you as an additional default.
This behavior allows the javac compiler to be called consistently for a project (set of source code files) independent of the directory the user that invoked the compiler. If it were any other way, then you would have to ensure that you always compiled from the same working directory to get the same results.
---- edit after seeing comment in ruakh's excellent answer ----
The second issue you are seeing isn't related to the first. The "Could not find or load main class" is because you are invoking the java command with the source code file name not the class name which is defined in that source code file.
The java command runs classes, not source code files. This makes more sense when you remember that a single source code file could contain more than one class (even if they typically don't).

Related

javac error: "Cannot find symbol"

I'm trying to compile java files on an EC2 instance, and am having trouble. I have several JAR files as well that are included in the classpath. The example would be a StockTrade.java (which is a stock trade object), which compiles completely without issue. In the same directory, there is the StockTradeGenerator.java, which will create StockTrade objects. When I try to compile this, it tells me it cannot find the StockTrade class (despite it being in the same directory already compiled. Specifically, there is a field within my StockTrade object that is a TradeType which is definied as an enum: BUY or SELL in the StockTrade.java code. It says it cannot find the symbol TradeType. My syntax is:
javac -cp lib/jar1.jar:lib/jar2.jar src/StockTradeGenerator.java
Does anyone know what is making it so that I cannot find TradeType when compiling StockTradeGenerator? As I said, its defined in StockTrade.java, which compiled without issue and is in the same folder/directory.
Put the current directory class path.
javac -cp .:lib/jar1.jar:lib/jar2.jar src/StockTradeGenerator.java

Cannot Find Symbol - Classes inside same package

I was having fun coding in Java, until for some reason javac stopped compiling my code. I googled about classpath, and set my CLASSPATH to /root/java. It was not set before that.
My package is in the /.../t directory with name centralServer (i.e. /.../t/centralServer).
Now it is compiling from the /.../t directory with the command javac centralServer/TestSSData.java. (The command is called from the /.../t directory). The output it gives is
Cannot find symbol
If I run the command from /.../t/centralServer with javac TestSSData.java I get as output:
File not found.
Point me the way for any roads I can follow to fix this please?
I removed the package statement and now it compiles. However this is not the way I would like to fix it...

Java code not compiling with 'javac' but compiles in Eclipse

I have just written a Java multi-threaded program in Eclipse. It compiled fine and works like a charm.
However, as this is coursework we are required to ensure that it compiles in the command line using 'javac' otherwise we score ZERO!
So, some classes compile others don't. The error I'm getting is the following ( they are all similar just with different class names, this is one example)
GateRunnable.java:7: cannot find symbol
symbol : class Station
location: class package.name.here.GateRunnable
public GateRunnable(Station st) {
^
Is this a javac issue? Any help appreciated.
Your compile -classpath and/or -sourcepath is incomplete. The compiler doesn't know where to find class Station. Here is a related question that describes how to set the classpath to include all the classes you want.
To solve the problem I was having, it was simply necessary to compile all classes by using the following command:
javac *.java
which compiles all java files in the directory.
have you compiled every .java file in your folder/packages? if not, then do so. Eclipse usually does that for you, but within the terminal it's you taking the responsibility of compiling every part of the code.

NoClassDefFoundError and Could not find or load main class

I am using the code from Rome's tutorials page http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader . Also trying this one: http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader
Compiling works, but I'm not sure how to run these examples. Why I just type java FeedReader or java FeedAggregator into the command line, I get the error:
C:\projects\freshmeat\src>java FeedAggregator http://freecode.com/?format=atom
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/f
eed/synd/SyndFeed
plus the large block that follows this error
Why is this happening, how do I fix it and try these things out? How do I get something to work with Rome!?
You need to include rome in the runtime classpath (in addition to the compile-time classpath)
java -classpath lib/rome.jar FeedAggregator ...
The samples you are trying to run are in the package com.sun.syndication.samples. You say you are a complete beginner, so, to make things simpler, I would recommend that you remove the line beginning with package in each of FeedReader.java and FeedAggregator.java. Recompile the classes after removing their package directives.
Then, to run these classes, make sure you're in the same directory as the class files FeedReader.class and FeedAggregator.class that javac created. Then, try running:
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. FeedReader
(and similarly for FeedAggregator.)
Note also that I've added the current directory, ., to the -cp attribute. Without this, the Java virtual machine won't know that it has to look in the current directory to find your FeedReader and FeedAggregator classes.
If you were to reinstate the package directives, you'd find the class files FeedReader.class and FeedAggregator.class would be created inside a directory com\sun\syndication\samples when you compile their sources. To run the class files from this location, you'd use a command line such as
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. com.sun.syndication.samples.FeedReader
and you'd run this from the directory containing the com subdirectory, not the directory that contains the class files.
More information on packages in Java can be found here.

"Cannot find symbol" for my own class

I do not have a %CLASSPATH% set up. As I understand, this should not be a problem because Javac will assume a classpath of the current directory.
As you can see below, javac is unable to find my Case class even though it's in the same exact directory. Any thoughts on why this is happening? This code works fine when I use Eclipse.
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>dir /B
Case.class
Case.java
EntryPoint.java
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>javac EntryPoint.java
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:24: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
cases.add(new Case(new Integer(count), line));
^
3 errors
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>
Update 1:
After trying to compile from my package root (src), I get a new error (even after deleting the Case.class file)
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -cp . codejam2011/Round0/D/EntryPoint.java
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
1 error
C:\Documents and Settings\joep\My Documents\GCJ\src>
Update 2:
It appears to be grabbing the Case.java file from a different package.
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -d ../classes codejam2011\Round0\D\*.java
.\codejam2011\Round0\D\Case.java:4: duplicate class: codejam2011.Round0.C.Case
public class Case
^
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
2 errors
C:\Documents and Settings\joep\My Documents\GCJ\src>
You need to compile from the package root, not from inside the package.
So, cd to the src folder and compile from there.
javac -cp . codejam2011/Round0/D/EntryPoint.java
Update: as per your new problem, you need to recompile Case.java the same way. It was apparently compiled the same wrong way (from inside the package).
If the problem is not yet solved by compiling from the package root directory (see the other answers):
make sure all the source files contain classes with names corresponding to their file name
make sure all the source files contain a package statement corresponding to their position in the source file hierarchy
delete all your .class files before compiling (this should only be necessary once, if you checked everything else).
Thus, if the file is codejam2011\Round0\D\Case.java, it should contain package codejam2011.Round0.D; as the first declaration, and then public class Case { ... }. Also, make sure there is no other source file containing this package and class declaration.
From your error message, it looks like the package statement is package codejam2011.Round0.C; instead (and you also have a class Case in the real codejam2011.Round0.C package).
You are in the wrong directory for compiling.
location: class codejam2011.Round0.D.EntryPoint
That tells me, that your package is codejam2011.Round0.D (which is against the convention (all lowercase) but beside the point ...
cd to the parent dir of codejam2011, which is src, isn't it?
javac codejam2011\Round0\D\EntryPoint.java
might do the trick.
Often you have a directory for compiled classes, like 'bin' or 'classes'. To produce the classes there, use -d (destination):
javac -d ../classes codejam2011\Round0\D\EntryPoint.java
I have similar issue, it might not apply to all cases, but what I have done is remove .gradle, build and out folder and rebuild the program again.

Categories