I am following a Java tutorial (left to my own devices to write the test code), but when trying to compile I get a symbol not found error. I've looked and looked, but cannot work out why the code I have written produces this error. It's probably very simple, but I'd appreciate someone pointing out the cause as I'm pulling my hair out trying to understand what I've done wrong!
TestBeerExpert.java:
package com.example.model;
import com.example.model.*;
import java.util.*;
public class TestBeerExpert {
public static void main(String[] args) {
TestBeerExpert test = new TestBeerExpert();
test.go();
}
private void go() {
BeerExpert expert = new BeerExpert();
List<String> brands = expert.getBrands("amber");
...
}
}
BeerExpert.java:
package com.example.model;
import java.util.*;
public class BeerExpert {
public List<String> getBrands(String color) {
List<String> brands = new ArrayList<String>();
...
return(brands);
}
}
Directory structure:
beerV1 -> src -> com -> example -> model -> TestBeerExpert.java & BeerExpert.java
Compiling from beerV1 with javac -d classes src/com/example/model/TestBeerExpert.java
And the actual error:
src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol
BeerExpert expert = new BeerExpert();
^
symbol: class BeerExpert
location: class TestBeerExpert
src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol
BeerExpert expert = new BeerExpert();
^
symbol: class BeerExpert
location: class TestBeerExpert
2 errors
For the life of me I can't work out what I'm doing wrong. The files are in the same directory and package, so as far as I am aware this should be compiling. I'd be grateful to now only have the code corrected but and explanation of what I have done wrong so I can remember it for the future. Thanks in advance.
Add src to your sourcepath
javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java
You need to do this because you execute javac from a different directory from where the sources are.
No need to compile BeerExpert first (javac will do it for you with the above command).
Validatation:
~/beerV1$ ls src/com/example/model/
BeerExpert.java TestBeerExpert.java
~/beerV1$ ls classes
~/beerV1$ javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java
~/beerV1$ ls classes/com/example/model/
BeerExpert.class TestBeerExpert.class
You are getting the compile error because compiler is not able to find BeerExpert class. Try below
1. first compile BeerExpert.java using :
javac -d classes src\com\example\model\BeerExpert.java
2. then compile TestBeerExpert.java
javac -cp classes -d classes src\com\example\model\TestBeerExpert.java
Related
I moved a class from Main.java into their own .java files and now the IDE (IntelliJ) can't find them, even though they are in the same package. Here are the first lines of Main...
package readability;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
var filePath = args[0];
var textAnalyser = new TextAnalyser(filePath);
... and here's the error I get when I run it from the command line:
C:\Users\123md\IdeaProjects\Readability Score\Readability Score\task\src\readability>java Main.java in.txt
Main.java:8: error: cannot find symbol
var textAnalyser = new TextAnalyser(filePath);
^
symbol: class TextAnalyser
location: class Main
Interestingly, when I just say String filePath = "in.txt" and run it in the console, it does find the class and runs fine, so why can't it find the class when I run it from the command line? Thanks!
You can only run single *.java file with java command. If your program uses mutiple files, you have to compile it first:
javac readability/Main.java readability/TextAnalyser.java
and then run:
java readability.Main in.txt
For more info see: https://openjdk.java.net/jeps/330
I am creating in my code a Java file, which I need to convert to a class. My file contains the following:
import com.company.tpch.TpchApplication;
import com.company.tpch.TpchApplicationBuilder;
import com.speedment.runtime.core.ApplicationBuilder;
public class java {
public void x() {
TpchApplication app = new TpchApplicationBuilder()
.withPassword("root")
.withLogging(ApplicationBuilder.LogType.STREAM)
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
.withSkipCheckDatabaseConnectivity()
.build();
}
}
When I try to compile the file with
javac -d . C:\Users\s\Desktop\demo\src\main\java\java.java
I have these errors:
import com.company.tpch.TpchApplication;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:2: error: package com.company.tpch does not exist
import com.company.tpch.TpchApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:3: error: package com.speedment.runtime.core does not exist
import com.speedment.runtime.core.ApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:7: error: cannot find symbol
TpchApplication app = new TpchApplicationBuilder()
^
symbol: class TpchApplication
location: class java
C:\Users\s\Desktop\demo\src\main\java\java.java:11: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
^
C:\Users\s\Desktop\demo\src\main\java\java.java:10: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.STREAM)
^
symbol: class TpchApplicationBuilder
location: class java
How can I solve this?
You need to add all classes that are used to your command. Like this:
javac com/company/tpch/TpchApplication com/company/tpch/TpchApplicationBuilder java.java
Of course you have to change this according to your directory structure, since I don't know it.
Anyway I would recommend an IDE that compiles your whole project at once like IntelliJ/Eclipse and/or a dependency tool like Maven/Gradle.
I'm trying to compile the following code (one of two files I need to complete this homework) but I'm getting 2 errors in cmd. This is what cmd throws at me:
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
2 errors
And this is the code I'm trying to compile.
public class CarRentalTest {
public static void main (String[] args)
{
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
myCarRental.Customers();
} //end method main
} //end class CarRentalTest
What's weird is that the whole thing runs fine in NetBeans. What am I doing wrong here? :9
What am I doing wrong here?
Not building CarRental, or not telling the compiler where to find the class if you have already compiled it. The IDE is probably assuming you want to build everything, so that's fine.
We don't know how your code is organized, but you should either pass all the relevant filenames to the compiler at the same time:
javac -d classes src\CarRental.java test\CarRentalTest.java
... or put the output directory of the earlier compilation in the classpath for the later compilation, e.g.
javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java
If you are using a standard directory layout for your project, where production and test code are in separate directory trees then the java command line will not see the production class if your currect directory is the test directory.
To clarify:
Suppose you have this dir structure:
src/
main/
java/
mypackage/
CarRental.java
test/
java/
mypackpage/
CarRentalTest.java
and you are in the 'src/test/java/mypackage/' directory, you would experience this error when running javac at the command line - although the production and test classes are in the same package, they are in different directories.
The IDE knows about this directory structure, includes the test path during compilation and therefore it works OK.
You need to import CarRental class in the CarRentalTest.
import yourpackage.CarRental in the CarRentalTest. Java Compiler can't find the CarRental in the CarRentalTest.java.
In the IDE whole package comes in the java file
import package.car.*;
This is why it is working in IDE.
I created multiple packages and want to compile and run them. I fiddled around with javac and java and learned about how packages should be named and how a project should be structured. I hope I got all right. But I fail at compilation and running the stuff. I know I could use an IDE for this, but I want to try it with the command-line tools just for curiousity.
Here is how my project is organized:
Project
+ src
+ net
+ chris
+ dojo
- Program.java
+ datastructures
- Queue.java
- LinkedList.java
+ sorting
- MergeSort.java
+ bin
+ net
+ chris
+ dojo
- Program.class (should be here but missing because compilation fails)
+ datastructures
- Queue.class
- LinkedList.class
+ sorting
- MergeSort.class
Compilation for the classes in the "datastructures" and "sorting" packages is working fine. Here are the commands I used. The folder structure in the "bin" folder is automatically created by the compiler.
javac -d bin src\net\chris\dojo\datastructures\*.java
javac -d bin src\net\chris\dojo\sorting\*.java
The problem is when I try to compile "Program.java" (thats the test class I run from the command-line) the compiler is throwing errors, because it cannot find the packages "net.chris.dojo.datastructures" and "net.chris.dojo.sorting".
Here is the compilation command:
javac -d bin src\net\chris\dojo\Program.java
This is the output I get:
src\net\chris\dojo\Program.java:3: error: cannot find symbol
import net.chris.dojo.datastructures;
^
symbol: class datastructures
location: package net.chris.dojo
src\net\chris\dojo\Program.java:4: error: cannot find symbol
import net.chris.dojo.sorting;
^
symbol: class sorting
location: package net.chris.dojo
src\net\chris\dojo\Program.java:11: error: cannot find symbol
MergeSort.sort(values);
^
symbol: variable MergeSort
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
Queue queue = new Queue();
^
symbol: class Queue
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
Queue queue = new Queue();
^
symbol: class Queue
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
LinkedList list = new LinkedList();
^
symbol: class LinkedList
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
LinkedList list = new LinkedList();
^
symbol: class LinkedList
location: class Program
7 errors
Thats the code of my class files:
Queue.java
package net.chris.dojo.datastructures;
public class Queue {
...
}
LinkedList.java
package net.chris.dojo.datastructures;
public class LinkedList {
...
}
MergeSort.java
package net.chris.dojo.sorting;
public class MergeSort {
...
}
Program.java
package net.chris.dojo;
import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;
public class Program {
public static void main(String[] args) {
int[] values = { 9, 4, 6, 2, 0, 3, 8, 1, 7, 5 };
MergeSort.sort(values);
Queue queue = new Queue();
LinkedList list = new LinkedList();
}
}
I would run it with this command:
java -cp bin net.chris.dojo.Program
I execute all commands in the root folder of the project.
Thanks for your help.
The solution was to include the classpath when compiling. That way it can find the packages it depends on.
javac -d bin -cp bin src\net\chris\dojo\Program.java
Thanks #BigMike for the solution.
Try change this in your Program class
import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;
to
import net.chris.dojo.datastructures.*;
import net.chris.dojo.sorting.*;
And when you compile your Program.java use following command
javac -d bin src\net\chris\dojo\Program.java -classpath bin
I'm trying to compile my class along with a provided .jar file which contains classes that my class will use.
This is what I've been trying:
javac -classpath .:WordSearch.jar WordSearchSolver.java
And this is the response:
WordSearchSolver.java:16: cannot find symbol
symbol : class PuzzleWord
location: class WordSearchSolver
public ArrayList<PuzzleWord> findwords()
^
WordSearchSolver.java:18: cannot find symbol
symbol : class PuzzleWord
location: class WordSearchSolver
return new ArrayList<PuzzleWord>();
^
2 errors
This is my class:
import java.util.ArrayList;
public class WordSearchSolver
{
public WordSearchSolver(int size, char[][] puzzleboard, ArrayList<String> words)
{
}
public ArrayList<PuzzleWord> findwords()
{
return new ArrayList<PuzzleWord>();
}
}
WordSearch.jar contains:
PuzzleUI.class
PuzzleWord$Directions.class
PuzzleWord.class
Natural.class
(WordSearchSolver.java and Wordsearch.jar are in the same directory)
Am I missing something?
Although you're on Cygwin, I'm guessing that your path separator should be a semicolon, since the Java compiler/JVM will be running in a Windows environment.
javac -cp .\;WordSearch.jar ...
Note that the semicolon must be escaped to prevent interpretation by the Cygwin shell (thanks to bkail below)
You aren't importing any of the classes from your WordSearch.jar in your WordSearchSolver class. You need import statements at the top of this class including their package.
It ended up being a combination of semicolons and quotation marks.
javac -classpath ".;WordSearch.jar" WordSearchSolver.java
Thanks everyone for pointing me in the right direction!