Error when compiling java file in cmd [duplicate] - java

From the Definitive ANTLR4 reference I have run through the first example and it has generated the JAVA target. In the directory C:\JavaLib I have antlr-4.5-complete.jar
When I attempt to compile it with;
javac -classpath C:\JavaLib *.java
It creates the following error messages;
helloBaseListener.java:13: error: class HelloBaseListener is public, should be declared in a file named HelloBaseListener.java
public class HelloBaseListener implements HelloListener {
^
helloListener.java:9: error: class HelloListener is public, should be declared in a file named HelloListener.java
public interface HelloListener extends ParseTreeListener {
^
helloParser.java:12: error: class HelloParser is public, should be declared in a file named HelloParser.java
public class HelloParser extends Parser {
^
helloBaseListener.java:3: error: package org.antlr.v4.runtime does not exist
import org.antlr.v4.runtime.ParserRuleContext;
^
helloBaseListener.java:4: error: package org.antlr.v4.runtime.misc does not exist
import org.antlr.v4.runtime.misc.NotNull;
^
helloBaseListener.java:5: error: package org.antlr.v4.runtime.tree does not exist
import org.antlr.v4.runtime.tree.ErrorNode;
....
What am I doing wrong?

There was 2 problems. One was the file has to be named "Hello.g4" not "hello.g4" because the grammar is specified as Hello. The second was the classpath, it requires the path and name of the jar file, as well as the current directory. The following command worked;
javac -classpath .;C:\JavaLib\antlr-4.5-complete.jar *.java

With regard to the above query re the colon separator then the answer is yes. I installed via the debian packages and used the command before working out how to set CLASSPATH
javac -classpath /usr/share/java/antlr4-runtime.jar Expr*.java
Before this I got a load of compile errors. Also it seems to be worth noting on debian at the moment my .bash_profile never gets loaded so I needed to put this in .bashrc

Related

How compile and run java project with multiple class using command line?

When I try to compile with javac .Main
D:\Desktop\Development\Java\Section 4\Abstract
❯ javac -classpath . *.java
error: Invalid filename: *.java
Usage: javac <options> <source files>
use --help for a list of possible options
[17:37]  Shell xUSAGE 174ms
Main.java:5: error: cannot find symbol
Student st1 = new Student("John");
^
symbol: class Student
location: class Main
Main.java:5: error: cannot find symbol
Student st1 = new Student("John");
^
symbol: class Student
location: class Main
2 errors
But I able to compile it succesfully with
D:\Desktop\Development\Java\Section 4\Abstract\src\com\Testing
>javac -classpath . *.java
Although I still haven't figured it out how to run the Main class
java Main
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: com/Testing/Main (wrong name: Main)
If you want to use straight javac to compile (really, don't - use a build system), you have to specify the files where they are on the file system. so not *.java, but src\com\Testing\*.java. Yes, if you have many packages, this means an extremely large command line.
java's argument takes a class name - a fully qualified one. Not a path.
If you have this source file:
package com.foo;
public class Bar {
public static void main(String[] args) { .. }
}
Then:
It should be in projectRoot/src/main/java/com/foo/Bar.java where projectRoot is whatever dir serves as your main project folder, src/main/java can be just src, or src/whatever - or something to indicate the kind of sub-project this covers, com/foo matches the package statement, and Bar matches the public class name.
When compiling a Bar.class falls out of it; this should be in projectRoot/bin/com/foo/Bar.class or similar, where bin might also be build. But the com/foo/Bar.class part is mandatory - class files have to be in a subdir structure that matches package.
To run this file, you would put projectRoot/bin on the classpath and then write out the full classname. So, if you're in the projectDir right now, java -cp bin com.foo.Bar.

Unable to import an enum from another class in the same package

I have a a class QueueType:
package mypack;
public enum QueueType {
SMTP, PUT, POST, ISF
}
I am trying to import this into another class in the same package but I get an error saying "can't find symbol QueueType". What am I doing wrong?
package mypack;
public class RawAsupFile {
private QueueType queueType;
.
.
.
If your files are in the same folder and you want to compile everything together then you can use with option javac *.java from folder/package location
In case if you want to compile each file individually then you can try with the command javac -cp <path location> filename.java.
Example javac -cp D:\mypack\* RawAsupFile.java in Windows

How do I run java projects from the terminal? [duplicate]

I am trying to compile (from the command line) a java package that imports another package of my own. I was following a tutorial online but it seems that I get an error when I try to compile the final java file (CallPackage.java).
Here is the file structure:
+ test_directory (contains CallPackage.java)
-> importpackage
-> subpackage (contains HelloWorld.java)
Here is CallPackage.java:
/// CallPackage.java
import importpackage.subpackage.*;
class CallPackage{
public static void main(String[] args){
HelloWorld h2=new HelloWorld();
h2.show();
}
}
and here is HelloWorld.java:
///HelloWorld.java
package importpackage.subpackage;
public class HelloWorld {
public void show(){
System.out.println("This is the function of the class HelloWorld!!");
}
}
Attempted Steps
Go to the subpackage and compile HelloWorld.java with $javac HelloWorld.java.
Go to test_directory and compile CallPackage.java with $javac CallPackage.java.
This gives me an error on the last command:
CallPackage.java:1: package importpackage.subpackage does not exist
import importpackage.subpackage.*;
^
CallPackage.java:4: cannot find symbol
symbol : class HelloWorld
location: class CallPackage
HelloWorld h2=new HelloWorld();
^
CallPackage.java:4: cannot find symbol
symbol : class HelloWorld
location: class CallPackage
HelloWorld h2=new HelloWorld();
^
3 errors
How can I compile both packages? Thanks so much for any help!
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
instead of going to subpackage, compile HelloWorld.java from the top_level:
$javac -cp . importpackage/subpackage/HelloWorld.java
compile CallPackage.java in the same way:
$javac -cp . CallPackage.java
run the file using the class path also:
$java -cp . CallPackage
NOTE: running "$java CallPackage" will give an error "Error: Could not find or load main class CallPackage"
In summary, during each step, the class path must be specified. It worked after running it as such.
Same situation to me. And I came to take over it by compiling classes at the same time.
For example, here is my project:
+ beerV1
-> classes
-> src
-> com
-> example
-> model
-> BeerExpert.java
-> web
-> BeerSelect.java
BeerExpert.java:
package com.example.model;
import ...
public class BeerExpert{
...
}
BeerSelect.java:
package com.example.web;
import com.example.model.*;
import ...
public class BeerSelect {
...
}
As you can see: BeerSelect.java is trying to import classes in com.example.model package.
At the first time, I compiled BeerExert.java first by command:
--> javac -d classes src/com/example/model/BeerExpert.java
Then:
--> javac -d classes src/com/example/web/BeerSelect.java
And the result was:
-->... error: package com.example.model does not exist
So, I knew that compiling multiple classes separately will not work in this case.
After suffering on google, I found this very simple way to solve the problem:
Just compile all at once:
--> javac -d classes src/com/example/model/BeerExpert.java src/com/example/web/BeerSelect.java
Finally, here is what I got:
+ beerV1
-> classes
-> com
-> example
-> model
-> BeerExpert.class
-> web
-> BeerSelect.class
-> src
-> com
-> example
-> model
-> BeerExpert.java
-> web
-> BeerSelect.java
Hope that helps.
Are you sure importpackage/subpackage is in your classpath?
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
If the -sourcepath option is not specified, the user class path is also searched for source files.
If the -processorpath option is not specified, the class path is also searched for annotation processors.
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
It's an old topic but Google has led me to this site. For the completness, I'd like to add one bit to #Vĩnh Thụy Trần's answer on how to run the main class after compiling it into a custom folder. While it looks trivial now it took me some time to get it right.
In order to run your project, you also need to specify a path to the classes:
java -classpath <directory> your.package.name.classname
or
java -cp <directory> your.package.name.classname
Taking Vĩnh Thụy Trần's example again, the command would look like this:
java -cp classes com.example.web.BeerSelect
I hope it will help someone as I spent some time figuring it out.
(1)first compile the code
javac -d importpackage.subpackage.HelloWorld
(2) and then compile the CallPackage.java
javac CallPackage.java
delete your package folder (after pasting you code to some other folder) and then locate to the folder in cmd where you current code is and try javac -d . Helloworld.java (this will create the Helloworldclass and subpackage as well) and try same for you mainfunction code ie Callpackage.java after compiling to run the code try java Callpackage

JFlex with CUP compile errors

I am trying to run an example provided by CUP: Parsing directly to XML.
I stored the 'Minijava Grammar' in a file named minijava.cup and the scanner into a file named xml.flex. I ran JFlex to obtain Lexer.java from the xml.flex file. After that I obtained Parser.java and sym.java after running the command specified on the CUP example:
java -jar java-cup-11b.jar -locations -interface -parser Parser -xmlactions minijava.cup
My directory looks like this:
input.xml
java-cup-11b.jar
java-cup-11b-runtime.jar
jflex-1.6.1.jar
Lexer.java
minyjava.cup
Parser.java
sym.java
xml.flex
I am trying to compile the Lexer.java file by using the following command:
javac -cp java-cup-11b-runtime.jar Lexer.java
but I get 47 errrors in the format "..cannot find symbol...". The first ones specify that classes sym and minijava.Constants can't be found.
Lexer.java:17: error: cannot find symbol
public class Lexer implements java_cup.runtime.Scanner, sym, minijava.Constants{
^ symbol: class sym
Lexer.java:17: error: package minijava does not exist
public class Lexer implements java_cup.runtime.Scanner, sym, minijava.Constants {
^ Lexer.java:679: error: cannot find symbol
{return symbolFactory.newSymbol("EOF", EOF, new Location(yyline+ 1,yycolumn+1,yychar), new Location(yyline+1,yycolumn+1,yychar+1));
I do not understand why the sym.java file is not visible to Lexer or where to get the minijava.Constants file.
You are missing the current directory (where your sources are) in the classpath. It is not included by default, but if you put . in the %CLASSPATH% (or $CLASSPATH for unices) environment variable.
Try to change the -cp setting to add the current directory ..
javac -cp .;java-cup-11b-runtime.jar Lexer.java
If you are on a GNU/Linux, OS X or any UNIX-like system, it would be
javac -cp .:java-cup-11b-runtime.jar Lexer.java
In the same way, add the current directory to the -cp parameter when running with java command.

getting error when I compile the Java code using package in commandline?

I have this directory structure:
project1/src/edu/course/firstweek/javacourse/Program1.java
Another file in one package above:
project1/src/edu/course/firstweek/program2.java
In the header of program2.java, I have
package edu.course.firstweek;
import edu.course.firstweek.javacourse.Program1;
Now to when I run the following in commandline:
Javac src/edu/course/firstweek/program2.java, I get this error:
src/edu/course/firstweek/program2.java:14:error cannot find symbol
System.out.println(program1.print("hello world"));
symbol: variable Program1
location: class program2
2 errors
I can see that the compiler is not able to find the program1,but I have the correct import package statement in program2. I need help here and after compiling, is there something that needs to be taken into account for running the program.
Thanks
Try going into one directory inside, i.e. cd src
and then compile Javac edu/course/firstweek/program2.java
for running, do java edu.course.firstweek.program2

Categories