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

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

Related

How to fix javac package does not exist?

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.

Packages, Files and Getting simple code to run!

I have SimpleSphere.java and TestClass.java stored in a folder called MyPackage.
Attempting to compile TestClass gives this error:
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
2 errors
But I am fairly certain I have everything set up correctly (evidently I do not, and yet I remain stubborn!). Also, even if these two files were not part of MyPackage, shouldn't JAVA look in the current directory as default and find SimpleSphere???
Seems that you're compiling the classes directly using javac ClassName.java inside the folder where they are located. You have to move one folder up and compile them since there.
Here's a sample of how the files should be located
- basePath
- MyPackage
+ SimpleSphere.java
+ TestClass.java
In your cmd/shell:
# [basePath] javac MyPackage/SimpleSphere.java
# [basePath] javac MyPackage/TestClass.java
# [basePath] java MyPackage.TestClass
Try moving one folder up and then compiling.
Best of Luck.

classpath issue in hadoop java

javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
appClientModule/grid/comp/tools/CleanTmp.java:2: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:3: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
appClientModule/grid/comp/tools/CleanTmp.java:4: package org.apache.commons.logging does not exist
import org.apache.commons.logging.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:5: package org.apache.commons.logging.impl does not exist
import org.apache.commons.logging.impl.Log4JLogger;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
appClientModule/grid/comp/tools/CleanTmp.java:9: cannot find symbol
symbol : class JobConf
location: class grid.comp.tools.CleanTmp
JobConf jconf;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
7 errors
tools $ javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
jabir:CompareHdfs jabir.ahmed$ javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
appClientModule/grid/comp/tools/CleanTmp.java:2: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:3: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
appClientModule/grid/comp/tools/CleanTmp.java:4: package org.apache.commons.logging does not exist
import org.apache.commons.logging.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:5: package org.apache.commons.logging.impl does not exist
import org.apache.commons.logging.impl.Log4JLogger;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
appClientModule/grid/comp/tools/CleanTmp.java:9: cannot find symbol
symbol : class JobConf
location: class grid.comp.tools.CleanTmp
JobConf jconf;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
7 errors
tools $ ls /installs/hadoop-0.20.2/lib/*.jar
/installs/hadoop-0.20.2/lib/ant-contrib-1.0b3.jar /installs/hadoop-0.20.2/lib/jasper-runtime-5.5.12.jar
/installs/hadoop-0.20.2/lib/aspectjrt-1.6.5.jar /installs/hadoop-0.20.2/lib/jets3t-0.6.1.jar
/installs/hadoop-0.20.2/lib/aspectjtools-1.6.5.jar /installs/hadoop-0.20.2/lib/jetty-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-cli-1.2.jar /installs/hadoop-0.20.2/lib/jetty-servlet-tester-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-codec-1.4.jar /installs/hadoop-0.20.2/lib/jetty-util-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-daemon-1.0.1.jar /installs/hadoop-0.20.2/lib/jsch-0.1.42.jar
/installs/hadoop-0.20.2/lib/commons-el-1.0.jar /installs/hadoop-0.20.2/lib/junit-4.5.jar
/installs/hadoop-0.20.2/lib/commons-httpclient-3.1.jar /installs/hadoop-0.20.2/lib/kfs-0.2.2.jar
/installs/hadoop-0.20.2/lib/commons-logging-1.0.4.jar /installs/hadoop-0.20.2/lib/log4j-1.2.15.jar
/installs/hadoop-0.20.2/lib/commons-logging-api-1.0.4.jar /installs/hadoop-0.20.2/lib/mockito-all-1.8.2.jar
/installs/hadoop-0.20.2/lib/commons-net-1.4.1.jar /installs/hadoop-0.20.2/lib/oro-2.0.8.jar
/installs/hadoop-0.20.2/lib/core-3.1.1.jar /installs/hadoop-0.20.2/lib/servlet-api-2.5-20081211.jar
/installs/hadoop-0.20.2/lib/hadoop-fairscheduler-0.20.2-cdh3u2.jar /installs/hadoop-0.20.2/lib/servlet-api-2.5-6.1.14.jar
/installs/hadoop-0.20.2/lib/hsqldb-1.8.0.10.jar /installs/hadoop-0.20.2/lib/slf4j-api-1.4.3.jar
/installs/hadoop-0.20.2/lib/jackson-core-asl-1.5.2.jar /installs/hadoop-0.20.2/lib/slf4j-log4j12-1.4.3.jar
/installs/hadoop-0.20.2/lib/jackson-mapper-asl-1.5.2.jar /installs/hadoop-0.20.2/lib/xmlenc-0.52.jar
/installs/hadoop-0.20.2/lib/jasper-compiler-5.5.12.jar## Heading ##
So how do i set the class path
I tried setting via the env variable too
$ echo $CLASSPATH
/installs/hadoop/lib/.jar:/installs/hadoop/.jar:/installs/hadoop-0.20.2/.jar:/installs/hadoop-0.20.2/lib/.jar
it still fails
$ javac -version
javac 1.6.0_29
Go for a more clear classpath like:
javac -classpath $HADOOP_HOME/hadoop-core-0.20.204.0.jar:$HADOOP_HOME/lib/commons-cli-1.2.jar -d inception src/Inception.java
(in this case Inception is your java program. don't know why i mentioned that but it's better to be more explicit then not!)
Most of the time you are going to need the hadoop-core-(yourversion) and the commons-cli if you are using the Tool interface.
If you have set the CLASSPATH variable, then you don't need to specify the -classpath flag in the javac command as that will take the classpath value from the environment variable CLASSPATH
The value of classpath needs to point to the actual jar file rather than .jar
For example,
set CLASSPATH=/myapp/mylib.jar
Depending on the platform you are on, you can either use : or ; as the delimiter between different paths. For Windows, use ; and UNIX normally uses :
In UNIX:
set CLASSPATH=/myapp/myclasses1.jar:/myapp/myclasses2.jar
But in Windows you would need to use semicolon as the delimiter between the paths
set CLASSPATH=/myapp/myclasses1.jar;/myapp/myclasses2.jar
inquire's solution helped me. Here is the syntax I used on HDInsight version 2.1:
C:\apps\dist\java\bin\javac -classpath %HADOOP_HOME%\hadoop-core-1.2.0.1.3.0.1-0302.jar;%HADOOP_HOME%\lib\commons-cli-1.2.jar WordCount.java
Instead of specifying individual libraries, just add " -classpath yarn classpath ". That makes the command pretty easy and simple.

java classpath cannot find symbol

My experience with Java: Read-Only
I have these lines in my code:
import com.altova.io.*;
import com.mapforce.*;
The packages are in Mapping.jar, which is on my classpath, indeed it is first. Javac -verbose admits this:
[search path for source files: Mapping.jar,.,[etc]
When the compiler gets to the use lines, however:
[loading com/altova/io/Input.class(com/altova/io:Input.class)]
ShapeTypeFiddle.java:339: cannot find symbol
symbol : class io
location: package com.altova
com.altova.io.StringInput(sthing.toString());
^
(+ two others, one is the MappingMapToinput2Output.run(input, output), the other is the output.getContent() call.)
Unzipping the Mapping.jar file does show compiled .class files for the Input.class, the MappingMapToinput2Output.class and the Output.class class files.
What else can I check?
It's looking for the class io which should have the static method StringInput. But you actually want to create an instance of StringInput. That can only mean that you forgot the new operator.
new com.altova.io.StringInput(sthing.toString());

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