How to fix javac package does not exist? - java

I am creating in my code a Java file, which I need to convert to a class. My file contains the following:
import com.company.tpch.TpchApplication;
import com.company.tpch.TpchApplicationBuilder;
import com.speedment.runtime.core.ApplicationBuilder;
public class java {
public void x() {
TpchApplication app = new TpchApplicationBuilder()
.withPassword("root")
.withLogging(ApplicationBuilder.LogType.STREAM)
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
.withSkipCheckDatabaseConnectivity()
.build();
}
}
When I try to compile the file with
javac -d . C:\Users\s\Desktop\demo\src\main\java\java.java
I have these errors:
import com.company.tpch.TpchApplication;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:2: error: package com.company.tpch does not exist
import com.company.tpch.TpchApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:3: error: package com.speedment.runtime.core does not exist
import com.speedment.runtime.core.ApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:7: error: cannot find symbol
TpchApplication app = new TpchApplicationBuilder()
^
symbol: class TpchApplication
location: class java
C:\Users\s\Desktop\demo\src\main\java\java.java:11: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
^
C:\Users\s\Desktop\demo\src\main\java\java.java:10: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.STREAM)
^
symbol: class TpchApplicationBuilder
location: class java
How can I solve this?

You need to add all classes that are used to your command. Like this:
javac com/company/tpch/TpchApplication com/company/tpch/TpchApplicationBuilder java.java
Of course you have to change this according to your directory structure, since I don't know it.
Anyway I would recommend an IDE that compiles your whole project at once like IntelliJ/Eclipse and/or a dependency tool like Maven/Gradle.

Related

junit - test won't run and receiving lots of errors

I have downloaded my old project that was built on a different computer. I have Java environment installed on the current one. I have also downloaded junit to get things work. When I run my tests using build.rb and run_test.rb - files that worked previously, I receive lots of errors negating the entire code. Here's the example
$ ruby build.rb
shop_basket\Cashdesk.java:4: error: class CashDesk is public, should be
declared in a file named CashDesk.java
public class CashDesk{
^
1 error
shop_basketSpec\CashdeskTest.java:5: error: class CashDeskTest is public,
should be declared in a file named CashDeskTest.java
public class CashDeskTest{
^
shop_basketSpec\BasketTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
shop_basketSpec\BasketTest.java:3: error: package org.junit does not exist
import static org.junit.Assert.*;
^
shop_basketSpec\CashdeskTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
shop_basketSpec\CashdeskTest.java:3: error: package org.junit does not exist
import static org.junit.Assert.*;
^
shop_basketSpec\CashdeskTest.java:7: error: cannot find symbol
CashDesk cashdesk;
^
symbol: class CashDesk
location: class CashDeskTest
shop_basketSpec\CustomerTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
shop_basketSpec\CustomerTest.java:3: error: package org.junit does not
exist
import static org.junit.Assert.*; ^
shop_basketSpec\ProductTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
and there are more errors like this as if they concerned the entire code structure. I don't get why.
All the CLASSPATH etc seems to be set on my windows OS. The entire thing irritates as I cannot move with my coding. Thanks for help
Here's my set CLASSPATH
CLASSPATH image
The content of my ruby files
run_tests.rb
require 'find'
def find_valid_files
files = []
Find.find('bin') do |path|
files << path if path.include?(".class") && path.include?("Test")
end
return files
end
def run_tests(files)
for file in files
fileName = File.basename(file, ".*")
puts "Running #{fileName}"
system("java org.junit.runner.JUnitCore #{fileName}")
end
end
valid_files = find_valid_files()
Dir.chdir "bin"
run_tests(valid_files)
build.rb
require 'fileutils'
def filter_directories
excluded_directories = ["bin"]
all_files = Dir.glob('*')
return all_files.select do |file|
next if excluded_directories.include?(file)
File.directory?(file)
end
end
def create_bin
FileUtils.rm_rf('bin')
FileUtils.mkdir_p('bin')
end
def run_tests directories
for directory in directories
puts "building #{directory}"
system("javac -d bin #{directory}/*.java")
end
end
create_bin()
valid_directories = filter_directories()
run_tests(valid_directories)
For the first two errors relating to CashDesk and CashDeskTest - your file names have a lowercase 'd' (Cashdesk.java and CashdeskTest.java). The casing in the file name should match the actual class names as declared in the code. Fix that.
If you still see JUnit errors afterwards, check that you have a JUnit JAR on your classpath.
Errors Cashdesk.java:4: error: class CashDesk is public, should be
declared in a file named CashDesk.java are caused by inconsistency between .java file names and public class which it contains. In this exaple class CashDesk is saved in Cashdesk.java file - note d vs D in Desk.
The second bunch of errors error: package org.junit does not exist mean you should include JUnit library to the classpath of your project

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 to include multiple class files and .Jar files in class path which exist in sub folders in linux?

How to include multiple class files and .Jar files in class path , i am on linux .. and want to integrate lucene packages downloaded with my code , all libraries and class files are in lucene dirc. ( they are multiple sub-directories inside that folder ) ?
HelloLucene.java:1: error: package org.apache.lucene.analysis.standard does not exist
import org.apache.lucene.analysis.standard.StandardAnalyzer;
^
HelloLucene.java:10: error: package org.apache.lucene.queryparser.classic does not exist
import org.apache.lucene.queryparser.classic.ParseException;
^
HelloLucene.java:11: error: package org.apache.lucene.queryparser.classic does not exist
import org.apache.lucene.queryparser.classic.QueryParser;
^
HelloLucene.java:23: error: cannot find symbol
public static void main(String[] args) throws IOException, ParseException {
^
symbol: class ParseException
location: class HelloLucene
HelloLucene.java:26: error: cannot find symbol
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
^
symbol: class StandardAnalyzer
location: class HelloLucene
HelloLucene.java:26: error: cannot find symbol
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
^
symbol: class StandardAnalyzer
location: class HelloLucene
HelloLucene.java:45: error: cannot find symbol
Query q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr);
^
symbol: class QueryParser
location: class HelloLucene
Note: HelloLucene.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
7 errors
Use below command:
export CLASSPATH="$CLASSPATH:xyz.jar**:**path_to_abc.jar.jar"
You basically have to append to the classpath, all the new entries that you want. To do so, just make use of ':' in linux and ';' in windows.
You can just add the paths to the folders where the jars are located and recursively the inner jars are picked.

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

Problem in compiling Java Source using ANTLR v3

I am trying to run ANTLR C grammar file (DummyC.g) from command line to parse C source and header files (a.h). When I run it with antlr.jar file, it generates parser and lexer files. but when I compile test file Main.java. It gives error of missing ANTLR packages as shown below.
C:\antlr-2.7.6\test>javac Main.java
Main.java:1: package org.antlr.tool does not exist
import org.antlr.tool.;
^
Main.java:2: package org.antlr.runtime does not exist
import org.antlr.runtime.;
^
Main.java:3: package org.antlr.runtime.tree does not exist
import org.antlr.runtime.tree.;
^
Main.java:4: package org.antlr.stringtemplate does not exist
import org.antlr.stringtemplate.;
^
Main.java:8: cannot find symbol
symbol : class CommonTree
location: class Main
CommonTree tree = DummyCParser.start("a.h");
Main.java
import org.antlr.tool.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class Main {
public static void main(String[] args) throws Exception {
CommonTree tree = DummyCParser.start("a.h");
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
}
What could be the problem?
You're using the antlr runtime so you'll have to specify the antlr jar files as part of the classpath so the compiler can find the antlr classes you use e.g.
javac -classpath c:\java\antlr-3.3\lib\antlr-3.3-complete.jar Main.java

Categories