Compiling java program with external libraries from command line - java

I am having difficulty compiling my java program from the command line. The program uses external libraries and I have had no issues compiling the program. However, upon running the program, the following error is generated:
C:\Users\...\App> javac -d bin -cp lib/json.jar src/*.java
C:\Users\...\App> java src/App.java
src\App.java:3: error: package org.json does not exist
import org.json.*;
^
src\App.java:35: error: cannot find symbol
JSONObject obj = new JSONObject(jsonString);
^
symbol: class JSONObject
location: class App
src\App.java:35: error: cannot find symbol
JSONObject obj = new JSONObject(jsonString);
^
symbol: class JSONObject
location: class App
3 errors
error: compilation failed
C:\Users\...\App>
The file structure looks like this:
How can I compile my program with its external libraries from the command line?
UPDATE
I've rearranged the file structure as advised and is now
However, I am still getting errors when compiling the program
C:\Users\...\App\src>javac -d ..\bin -cp lib\json.jar App.java
C:\Users\...\App\src>java -cp ..\bin:.\lib\json.jar App.java
App.java:3: error: package org.json does not exist
import org.json.*;
^
App.java:35: error: cannot find symbol
JSONObject obj = new JSONObject(jsonString);
^
symbol: class JSONObject
location: class App
App.java:35: error: cannot find symbol
JSONObject obj = new JSONObject(jsonString);
^
symbol: class JSONObject
location: class App
3 errors
error: compilation failed

It's not java xxx.java, it's java SomeNameoOfTheClassFileName.
You can just click the add button on the right of Referenced Libraries under the Java Projects panel:
Then you can import the org.json.* successfully. Then you can click the Run Java button on the top-right of the VSCode, the Java extension will help you compile the java code automatically.
Or you just want to compile the java file manually, you can read this article.
Depending on the first picture's structure it will be like this:
First
javac -cp lib\json.jar -d classes src\App.java
After that, create a manifest.txt next to README.md and contains:
Main-Class: App //if your App.java has package src, it will be src.App
Class-Path: lib\json.jar
//need keep a blank line here
Then
jar cfm App.jar manifest.txt -C classes .
Last
java -jar App.jar

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.

Error when compiling java file in cmd [duplicate]

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

Unable to compile .java file using javac at Windows command prompt

I am trying to compile LoginTest_Chrome.java using javac at Windows command prompt. Prior to this, I have set my Environment's classpath to be associated with all the .jar library files, such as Apache POI and Selenium.
Using echo %classpath%, this is the result of my classpath Environment:
Then, I execute javac LoginTest.java and I got the following result:
Hope to have advice from experts here on how to resolve this issue which has bugged me for two days.
Error message after javac *.java:
Start.java:63: error: cannot find symbol tc = row.getCell(0).toString();
^ symbol: variable tc location: class Start
Start.java:64: error: cannot find symbol username = row.getCell(1).toString();
^ symbol: variable username location: class Start
Start.java:65: error: cannot find symbol password = row.getCell(2).toString();
^ symbol: variable password location: class Start
The problem is not with the compiler itself but with your source code. If you take a look at the first Error it says
LoginTest_Chrome.java:76: error cannot find symbol
LoginCredentials.getFile();
which means that the LoginCredentials variable you are trying to use at line 76 wasn't declared somewhere, or it's somewhere the compiler can't see it (in another .java file maybe?)
All the other errors mean the same.

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