javac fails with symbol not found - java

Here is my folder structure:
- myGame/
- bin/
- src/
- common/
- logic/
- main/
- messageprotocol/
- test/
- util/
- Test.java
- Rectangle.java
- Geometry.java
I would like to compile Test.java which is in util package. However, I get symbol not found errors, I guess javac command is wrong. Also, you should know that Test.java has references to other classes within the same project (myGame).
Here is what I do:
cd myGame/
javac -d bin -classpath bin src/util/Test.java
Once I'm done with above, I'd do this and it would work (I hope):
cd myGame/bin
java Test
I have read a lot of resources and tried to apply but it didn't work for my folder structure. What am I doing wrong?
Edit: Errors I receive are:
myGame/src/util/Test.java:16: cannot find symbol
symbol : class Rectangle
location: class util.Test
private static final Rectangle[] rectangles = new Rectangle[someNumber];
^
myGame/src/util/Test.java:16: cannot find symbol
symbol : class Rectangle
location: class util.Test
private static final Rectangle[] rectangles = new Rectangle[someNumber];
^
myGame/src/util/Test.java:20: cannot find symbol
symbol : variable Geometry
location: class util.Test
System.out.println("Printing " + Geometry.NUMBER);
^
myGame/src/util/Test.java:24: cannot find symbol
symbol : variable Geometry
location: class util.Test
serverSocket = new Circle(Geometry.NUMBER);
^
myGame/src/util/Test.java:36: cannot find symbol
symbol : class Rectangle
location: class util.Test
rectangles[i] = new Rectangle(number, rectangles);
^
5 errors

Package structure for your Test.java is util.Test so you should do:
cd myGame/src
javac -d ../bin -classpath ../bin util/Test.java
and then try running it. hope this helps.

Related

Trouble Compiling and Running Java console application OS X Terminal

Cannot compile or run the Hangman console application I created in Eclipse. It is in my ~/Documents folder on my mac in a package called hangman. It doesn't see the two classes Game and Prompter that I created. I've tried using -cp but I'm not sure I am doing it right. It doesn't the ways I've tried it. Any help?
HoltnetMacbookAir:hangman godmanliving$ javac -classpath . Hangman.java
Hangman.java:20: error: cannot find symbol
Game game = new Game(args[0]);
^
symbol: class Game
location: class Hangman
Hangman.java:20: error: cannot find symbol
Game game = new Game(args[0]);
^
symbol: class Game
location: class Hangman
Hangman.java:21: error: cannot find symbol
Prompter prompter = new Prompter(game);
^
symbol: class Prompter
location: class Hangman
Hangman.java:21: error: cannot find symbol
Prompter prompter = new Prompter(game);
^
symbol: class Prompter
location: class Hangman
4 errors
You need to specify all files that need to be compiled.
It looks like in your case it would be something like this:
javac -classpath . Hangman.java Game.java Prompter.java
References:
how to compile multiple java source files in command line
http://www.codejava.net/java-core/tools/using-javac-command

Packages, Files and Getting simple code to run!

I have SimpleSphere.java and TestClass.java stored in a folder called MyPackage.
Attempting to compile TestClass gives this error:
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
2 errors
But I am fairly certain I have everything set up correctly (evidently I do not, and yet I remain stubborn!). Also, even if these two files were not part of MyPackage, shouldn't JAVA look in the current directory as default and find SimpleSphere???
Seems that you're compiling the classes directly using javac ClassName.java inside the folder where they are located. You have to move one folder up and compile them since there.
Here's a sample of how the files should be located
- basePath
- MyPackage
+ SimpleSphere.java
+ TestClass.java
In your cmd/shell:
# [basePath] javac MyPackage/SimpleSphere.java
# [basePath] javac MyPackage/TestClass.java
# [basePath] java MyPackage.TestClass
Try moving one folder up and then compiling.
Best of Luck.

Java: Compiling and running multiple packages from the command-line

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

How do I add my jar to Java compilation path?

I'm very new to Java programming and I'm trying to add jar to the classpath like this:
javac -classpath ~/Downloads/algs4.jar. ThreeSum.java
but still I'm getting the exception like:
ThreeSum.java:38: error: cannot find symbol
StdOut.println(a[i] + " " + a[j] + " " + a[k]);
^
symbol: variable StdOut
location: class ThreeSum
ThreeSum.java:62: error: cannot find symbol
int[] a = In.readInts(args[0]);
^
symbol: variable In
location: class ThreeSum
ThreeSum.java:64: error: cannot find symbol
Stopwatch timer = new Stopwatch();
^
symbol: class Stopwatch
location: class ThreeSum
ThreeSum.java:64: error: cannot find symbol
Stopwatch timer = new Stopwatch();
^
symbol: class Stopwatch
location: class ThreeSum
ThreeSum.java:66: error: cannot find symbol
StdOut.println("elapsed time = " + timer.elapsedTime());
^
symbol: variable StdOut
location: class ThreeSum
ThreeSum.java:67: error: cannot find symbol
StdOut.println(cnt);
^
symbol: variable StdOut
location: class ThreeSum
6 errors
I'm trying the programs from here
Looks like you need to download this JAR as well: http://introcs.cs.princeton.edu/java/stdlib/
They don't have a package structure, so unpack everything in the same directory:
jar xvf stdlib.jar
jar xvf algs4.jar
Then compile it:
javac -classpath . ThreeSum.java
You probably want ~/Downloads/algs4.jar. for that classpath to be ~/Downloads/algs4.jar:. on Linux or ~/Downloads/algs4.jar;. on Windows`

Java cannot find symbol when compiling

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

Categories