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());
Related
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
I'm trying to compile the following code (one of two files I need to complete this homework) but I'm getting 2 errors in cmd. This is what cmd throws at me:
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
2 errors
And this is the code I'm trying to compile.
public class CarRentalTest {
public static void main (String[] args)
{
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
myCarRental.Customers();
} //end method main
} //end class CarRentalTest
What's weird is that the whole thing runs fine in NetBeans. What am I doing wrong here? :9
What am I doing wrong here?
Not building CarRental, or not telling the compiler where to find the class if you have already compiled it. The IDE is probably assuming you want to build everything, so that's fine.
We don't know how your code is organized, but you should either pass all the relevant filenames to the compiler at the same time:
javac -d classes src\CarRental.java test\CarRentalTest.java
... or put the output directory of the earlier compilation in the classpath for the later compilation, e.g.
javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java
If you are using a standard directory layout for your project, where production and test code are in separate directory trees then the java command line will not see the production class if your currect directory is the test directory.
To clarify:
Suppose you have this dir structure:
src/
main/
java/
mypackage/
CarRental.java
test/
java/
mypackpage/
CarRentalTest.java
and you are in the 'src/test/java/mypackage/' directory, you would experience this error when running javac at the command line - although the production and test classes are in the same package, they are in different directories.
The IDE knows about this directory structure, includes the test path during compilation and therefore it works OK.
You need to import CarRental class in the CarRentalTest.
import yourpackage.CarRental in the CarRentalTest. Java Compiler can't find the CarRental in the CarRentalTest.java.
In the IDE whole package comes in the java file
import package.car.*;
This is why it is working in IDE.
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.
Currently trying to work with objects in Java. Everything goes fine until I hit compile. Have been reading a couple of other questions regarding the same problem, or the same given error, and at this point I am not sure wether I am forgetting something or that I need to change my classpath.
Main Class file:
package TesterClass;
public class Tester {
public static void main(String[] args){
TesterClass firstTest = new TesterClass();
firstTest.stringPrinter();
}
}
The file that is supposed to be functioning as a package file:
package TesterClass;
public class TesterClass{
private String workingSegment;
public TesterClass(){
workingSegment = "Working";
}
public void stringPrinter(){
System.out.println(workingSegment);
}
}
The 2 files are in the same directory and I am trying to manually compile them with
"javac Tester.java". The error I get is about the fact that its having issues with the package. All help is welcome!
EDIT: Forgot to post the actual compiler error.
Tester.java:9: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
TesterClass firstTest;
^
Tester.java:11: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
firstTest = new TesterClass();
^
2 errors
Move to the top of the source tree and compile both class...
So, assuming you source files are in \Java\TesterClass, you need to start in \Java
javac TesterClass\Tester.java TesterClass\TesterClass.java
You may also want to have a quick read of Code Conventions for the Java Programming Language as packages names are suppose to be in lower case :P
Updated
I just tried...
javac TesterClass\Tester.java
And it worked fine.
Are you sure that the Tester.java and TesterClass.java are in the TesterClass directory?
Updated with running example
So, basically, I dropped you .java files into the directory \compile under the TesterClass (\compile\TesterClass) directory and compiled them using...
\compile>javac TesterClass\Tester.java
Then I run them...
\compile>java TesterClass.Tester
Working
You need to go to the top of the directory hierarchy and first compile your TesterClass and then compile your Tester. Since you have not compiled your TesterClass yet, Tester is unable to find it.
The error clearly states that its not able to find the symbol TesterClass, and the reason being TesterClass hasn't been compiled yet.
I suggest you use an IDE which does the compilation automatically for you. If you stick to manual compilation, you need to compile all the classes in the proper order.
Try changing the package name so it does not match the class name. Right now they are the same. Make it package TesterClassPackage, then import TesterClass into the file with the main() method. Even though they are in the same package sometimes you need to literally import files even though they are in the same package.
javac TesterClass\TesterClass.java TesterClass\Tester.java
will do it
I'm trying to compile my class along with a provided .jar file which contains classes that my class will use.
This is what I've been trying:
javac -classpath .:WordSearch.jar WordSearchSolver.java
And this is the response:
WordSearchSolver.java:16: cannot find symbol
symbol : class PuzzleWord
location: class WordSearchSolver
public ArrayList<PuzzleWord> findwords()
^
WordSearchSolver.java:18: cannot find symbol
symbol : class PuzzleWord
location: class WordSearchSolver
return new ArrayList<PuzzleWord>();
^
2 errors
This is my class:
import java.util.ArrayList;
public class WordSearchSolver
{
public WordSearchSolver(int size, char[][] puzzleboard, ArrayList<String> words)
{
}
public ArrayList<PuzzleWord> findwords()
{
return new ArrayList<PuzzleWord>();
}
}
WordSearch.jar contains:
PuzzleUI.class
PuzzleWord$Directions.class
PuzzleWord.class
Natural.class
(WordSearchSolver.java and Wordsearch.jar are in the same directory)
Am I missing something?
Although you're on Cygwin, I'm guessing that your path separator should be a semicolon, since the Java compiler/JVM will be running in a Windows environment.
javac -cp .\;WordSearch.jar ...
Note that the semicolon must be escaped to prevent interpretation by the Cygwin shell (thanks to bkail below)
You aren't importing any of the classes from your WordSearch.jar in your WordSearchSolver class. You need import statements at the top of this class including their package.
It ended up being a combination of semicolons and quotation marks.
javac -classpath ".;WordSearch.jar" WordSearchSolver.java
Thanks everyone for pointing me in the right direction!