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

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.

Related

Compiling java program with external libraries from command line

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

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

antlr error in (CommonTree)parser.javaSource() line

I have used ANTLR to parse Java code and everything works well. But the problem is getting the following error when I run it in on a Mac:
javac -cp antlrworks-1-1.4.3.jar *.java
Main.java:18: cannot find symbol
symbol : method javaSource()
location: class JavaParser
CommonTree tree = (CommonTree)parser.javaSource().getTree();
^
1 error
The Java grammar you are using does not have a rule called javaSource, so no method with that name exists in the generated code. Some other Java grammars I have seen use compilationUnit, but without seeing your grammar there's no way to tell you exactly how to fix this.

Getting cannot find symbol error while compiling the java source from command prompt?

I am trying to compile my SOAuthServiceImpl.java but i get below error . I have already set the class path to location where XWikiConfig classfile lies
i.e C:\tomcat-6.0.26.B-RELEASE\webapps\wiki\WEB-INF\lib; Even when i echo classpath on command prompt i see same class path but still it is not able to
find symbol XWikiConfig. Though jar file(named as xwiki-platform-legacy-oldcore-3.3.jar)at location C:\tomcat-6.0.26.B-RELEASE\webapps\wiki\WEB-INF\lib
contains the file XWikiConfig(under com.xpn.xwiki).
C:\XWiki\trunk\src\main\java\com\xpn\xwiki\user\impl\xwiki>javac SSOAuthSe
rviceImpl.java
SSOAuthServiceImpl.java:3: cannot find symbol
symbol : class XWikiConfig
location: package com.xpn.xwiki
import com.xpn.xwiki.XWikiConfig;
^
SSOAuthServiceImpl.java:4: cannot find symbol
symbol : class XWikiContext
location: package com.xpn.xwiki
import com.xpn.xwiki.XWikiContext;
Not getting what i am missing here?
you need to add path till .jar in classpath adding path to jar won't do

Categories