Java Symbol not found through javac weird error (Runs through Eclipse) - java

My project runs smoothly in Eclipse, no errors at all. I ran it through local tests (that are equal to the ones in an online mooshak contest) and all checks out. But when I sumbit it online, I get a CompileTimeError.
To try to locate the problem, I attempted to use javac in Main.java, and this happened:
javac -encoding US-ASCII Main.java
Main.java:8: error: package code does not exist
import code.*;
^
Main.java:129: error: cannot find symbol
public static void addWords(Scanner in, LibSystem system, int wordCount)
{
^
symbol: class LibSystem
location: class Main
Main.java:153: error: cannot find symbol
public static void searchWord(LibSystem system, String word) {
^
Other symbol errors follow for every reference to classes from my only package, which I fully import and the header of Main, for a total of 29 errors.
Project
src
Main.java
code (package with all my other java files)

Follow these steps :-
1.) make sure LibSystem class is in classpath.
2.)Compile java files in code folder using -d option. That will create directories with classes.
javac -d . code/*.java
3.) then compile your Main class.
javac Main.java

Related

Java cannot find package when importing

I have a file structure as follows:
src/cs/example/Hello.java
src/cs/utility/HelloHelp.java
bin/cs/example/Hello.class
bin/cs/utility/HelloHelp.class
The package and import statements in Hello.java are:
package cs.example;
import cs.utility.MyMethods;
While the package statement in HelloHelp is:
package cs.utility;
I attempt to run Hello (that uses a method from HelloHelp):
java -cp src/cs/utility src/cs/example/Hello.java
src/cs/example/Hello.java:2: error: package cs.utility does not exist
import cs.utility.HelloHelp;
^
src/cs/example/Hello.java:10: error: cannot find symbol
int max = HelloHelp.borp(intOne,intTwo);
^
symbol: variable HelloHelp
location: class Hello
Any help on resolving this issue would be very useful, thank you!
In your java -cp call you are pointing to your .java files instead of your .class files in /bin.
Try changing your java command to use the class files instead. Something like the following should work:
java -cp "bin/*" cs.example.Hello

How to run StanfordCoreNlpDemo.java

I successfully compiled StanfordCoreNlpDemo by running:
javac -cp "*" StanfordCoreNlpDemo.java
and it compiled successfully. I then tried to run it with:
java -cp "*" StanfordCoreNlpDemo
I then received the following error:
Error: Could not find or load main class StanfordCoreNlpDemo
I realize this is a CLASSPATH issue so I tried to add the path to the folder:
/some/path/stanford-corenlp-full-2016-10-31/*
Nonetheless, I still get the same error. How do I run StanfordCoreNlpDemo.java?
This is not a problem of StanfordCoreNlpDemo program because I ran that code in Netbeans before. The problem seems associated with classpath issue.
Since the StanfordCoreNlpDemo.java file belongs to a package
package package edu.stanford.nlp.pipeline.demo;
public class StanfordCoreNlpDemo {
public static final void main(String[] args) throws IOException {
// code goes here
}
}
Then calling the following results in Error: Could not find or load main class TheClassName.
java -cp . StanfordCoreNlpDemo
It must be called with its fully-qualified name:
java -cp . edu.stanford.nlp.pipeline.demo.StanfordCoreNlpDemo
And this edu.stanford.nlp.pipeline.demo directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which edu.stanford.nlp.pipeline.demo exists.
Reference
https://stackoverflow.com/a/29331827/5352399
https://stackoverflow.com/a/18093929/5352399

Yet another 'cannot find symbol' error when creating a new class object

In short, I'm trying to instantiate within the main method in order to handle computations. I wrote the main class in Eclipse and was able to compile and run everything smoothly.
Main Method:
public static void main(String[] args)
{
...
OutsideClass class = new OutsideClass();
...
}
I ran it in eclipse, which worked smoothly until I got an error due to to insufficient privileges, which led me to switch over to using cmd.exe as an administrator.
I navigated to the eclipse folder where I had all the classes saved to and ran javac x.java for each file in the folder, one by one. I was able to do javac OutsideClass.java without any errors, though when it came to javac Main.java, I received the following error:
Main.java:36: error: cannot find symbol
OutsideClass outside = new OutsideClass();
^
symbol: class OutsideClass
location: class Main
Main.java:36: error: cannot find symbol
OutsideClass outside = new OutsideClass();
^
symbol: class OutsideClass
location: class Main
2 errors
The OutsideClass doesn't have a defined constructor, though I don't know if that really matters or not.
The Java compiler needs the source (.java) or bytecode (.class) of OutsideClass when compiling Main.java.
Try javac *.Java, or javac -cp OutsideClass.class Main.java to provide OutsideClass's definition to the compiler when compiling Main.
It is more customary for Java developers to compile all Java sources of a single project via one javac invitation, either directly, or via a tool such as Maven.

Cannot find symbol (extends another class)

I have my file structure as (they are in the package assigment):
prog/
src/assignment/
bin/assignment/
lib/assignment/
The files I am compiling are in src, and are being compiled to bin. The command I type is javac -d ./bin/ -cp ./bin/:./src/ -target 1.7 ./src/assignment/*.java
I have also tried javac -d ./bin/ -cp ./bin/:./src/ -target 1.7 ./src/assignment/GoodBrain.java ./src/assignment/LameBrain.java ./src/assignment/Board.java
For some reason I cannot two class files, the error message is 'cannot find symbol' (it points to the Board class and the LameBrain class).
The error message:
javac -g -d ./bin/ -cp ./src/:. -target 1.7 src/assignment/GoodBrain.java
src/assignment/GoodBrain.java:3: error: cannot find symbol
public class GoodBrain extends LameBrain{
^
symbol: class LameBrain
src/assignment/GoodBrain.java:11: error: cannot find symbol
public double rateBoard(Board board) {
^
symbol: class Board
location: class GoodBrain
2 errors
make: *** [bin/assignment/GoodBrain.class] Error 1
It is the same error for all the commands that I type in. Please tell me if I need to provide any new information
I fixed the error, it was because I forgot to include the package statement. Very dumb mistake

Compiling Java classes in linux via command line

Hi and thanks for taking the time to answer my questions.
I have two files in my root folder (~/). The Main.Java and TestMain.java. Main.java compiles and runs smoothly. TestMain on the other hand does not. TestMain is basically a test class where I use JUnit to handle different scenarios. I instantiate Main in TestMain but the problem is that the compiler cannot find Main.java.
Here's the code:
user#linuxbox ~ $ javac -cp junit-4.10.jar TestMain.java
TestMain.java:8: error: cannot find symbol
Main mainClass = new Main();
^
symbol: class Main
location: class TestMain
TestMain.java:8: error: cannot find symbol
Main luckyStrings = new Main();
^
symbol: class Main
location: class TestMain
2 errors
How can I make the Main class available to the MainTest.java class? Thanks so much!
In your classpath option, you have set the classpath to only junit-4.10.jar. You must also include the current directory where your Java files reside.
javac -cp "junit-4.10.jar:." TestMain.java
This includes two paths -- JUnit and the current directory, separated by a :. (If this were Windows, then you would use a ; as a separator).
Just another input...
-d can be used to specify the target directory where the compiled class files should be put
javac -d . -cp "junit-4.10.jar:." TestMain.java

Categories

Resources