Similar to other questions on this subject, I am having problems getting JUnit 4.11 to recognize my class.
Similar to the question found here, I am using algs4.jar to write a class, Percolation, which is all in the default package. Just as in the post I linked, I too had difficulty importing algs4 into my class to use the required WeightedWuickUnionUF class found within. I was finally able to compile the program with the following setup.
> echo $CLASSPATH
/Users/dj/Library/Java/Extensions/algs4.jar:
/Users/dj/Library/Java/Extensions/hamcrest-core-1.3.jar:
/Users/dj/Library/Java/Extensions/junit-4.11.jar:
/Users/dj/Library/Java/Extensions/stdlib.jar
Percolation.java
import java.util.Arrays;
import algs4.*;
public class Percolation {
private WeightedQuickUnionUF union_find_ds;
...
}
test_Percolation.java
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.Assert.*;
#RunWith(JUnit4.class)
public class test_Percolation{
private static Percolation perc;
#BeforeClass
public static void testSetup(){
perc = new Percolation(5);
}
#AfterClass
public static void testCleanup(){
perc = null;
}
#Test(expected = IndexOutOfBoundsException.class)
public void testIndexErrorThrown(){
perc.open(0,5);
}
}
Compilation was accomplished with the following commands. Both .java files are in the same folder, so too is a directory titled algs4.jar with the algs4.jar fully expanded such that each class exists as its own file. Therefore in the working directory there are the following files.
Percolation.java
test_Percolation.java
algs4/WeightedQuickUnionUF.class
I feel that this is an important point because even though algs4.jar is within the classpath, the Percolation.java file ONLY compiles when I have algs4/WeightedQuickUnionUF.class in the current working directory. For some reason I cannot get the import to work with the .jar in the classpath.
>javac Percolation.java
>javac test_Percolation.java
Please note that in Percolation.java if I changed the import line from import algs4.*; to import algs4.WeightedQuickUnionUF; I receive the following error at compile time.
Percolation.java:23: error: cannot access WeightedQuickUnionUF
import algs4.WeightedQuickUnionUF;
^
bad class file: ./algs4/WeightedQuickUnionUF.class
class file contains wrong class: WeightedQuickUnionUF
Please remove or make sure it appears in the correct subdirectory of the classpath.
Having compiled both files successfully, I attempted to use the solutions found from the following question. Entering the command java org.junit.runner.JUnitCore test_Percolation or the more verbose but seemingly unecessary command java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore test_Percolation both yield the following error.
JUnit version 4.11
Could not find class: test_Percolation
Time: 0.002
OK (0 tests)
I suspect that there is something wrong with my system configuration on top of the effects of my limited knowledge of Java and importing using Java. I know these questions have been posted elsewhere, but the solutions to said questions have not worked on my machine. Perhaps I have missed a nuance of the answers to other questions.
I am using Mac OSX 10.8.5 doing all of the programming and compilling from the command line using javac 1.7.0_07.
Look at your command-line:
java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
You've got the JUnit jar file on your class path - but not the current directory. So it won't be able to find any classes that aren't either "system" classes or in JUnit.
Try something like:
java -cp .:/Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
Ideally you should also start using packages though - and using more conventional class names. (test_Percolation violates normal conventions.)
Related
I am trying to run a simple doclet program, but I am not able to compile it.
javac -cp /cygdrive/c/Progra~2/Java/jdk1.8.0_65/lib/tools.jar A.java
But it throws
A.java:1: error: package com.sun.javadoc does not exist import
com.sun.javadoc.ClassDoc;
Where A.java is
import com.sun.javadoc.ClassDoc;
public class A {
}
I referred it from
http://download.java.net/jdk7u2/docs/technotes/guides/javadoc/doclet/overview.html
I know that I am doing a simple mistake but I ma not able to figure it out.
Can anyone please point me out what am I doing wrong
You need to add Tools.jar to the project path. It is not included in the standard installation.
Can I ask why you need com.sun.javadoc? It is in most cases discouraged to use com.sun overall.
I have several class files in a project (residing in the default package). My teacher needs me to email him the java files only, and these files need to compile on his computer without errors. To test this out, I copied my class files to another folder and ran javac myself. I get errors when I try to compile that say package org.junit does not exist import static or.junit.Assert.*; There are other errors as well but I'm assuming they're probably related to that first one.
Most of the class files in my directory are indeed test classes that I'm using to test methods with JUNIT. What files do I need to include or what changes do I need to make so that anyone could simply use javac and compile my java files?
Here's a sample of one of my classes:
import static org.junit.Assert.*;
import org.junit.Test;
public class SetRadiusTest {
#Test
public void test() {
Circle test = new Circle(200, 0, 0);
test.setRadius(200);
assertEquals(100, test.getRadius(), 1);
test.setRadius(50);
assertEquals(50, test.getRadius(), 1);
}
}
Check the java class path and it will tell you the dependencies you need to compile your application. running javac at the command line acts differently than when it is automatically compiled by an IDE like eclipse if the class paths do not match. For commonly used jar files like JUnit your teacher should probably already have it in their environment. You should only include need to include jar files which are not part of a common configuration.
I'm trying to use guava with j2objc but I'm getting the error:
j2objc TestJava.java
translating TestJava.java
error: TestJava.java:1: The import com.google.common cannot be resolved
error: TestJava.java:10: Lists cannot be resolved
Translated 0 files: 2 errors, 0 warnings
while running:
j2objc Test.java
where Test.java contains:
import com.google.common.collect.Lists;
import java.util.ArrayList;
public class Test {
public static void TestMethod() {
ArrayList<String> objects = Lists.newArrayList();
objects.add(0, "Hello world");
System.out.println(objects);
}
}
I've downloaded the lastest release 0.9.5 and added it to .profile:
export PATH=$HOME/bin/j2objc-0.9.5:$PATH
What else do I need to do in order to use guava?
Thanks!
The j2objc translator uses a Java compiler as its front-end, so the same classpath and sourcepath needs to be used when translating as when compiling to Java classes. Here you'll get a similar error if you ran "javac Test.java", because the guava jar needs to be included in the classpath. The j2objc distribution includes lib/guava-jdk5.jar, so run "javac -classpath /lib/guava-jdk5.jar Test.java", and fix any problems with the directory for the guava jar if necessary. Once javac can compile it, substitute "j2objc" for "javac" using the same arguments, and it should translate fine.
You don't always have to use javac first, but whenever there are translation errors reported, it's a quick test to see whether the issue is related to a missing source or class path.
One difference from Java is that when you link the application, the -lguava flag is needed to include that library.
I am trying to call a python method in java, similar functionality as Unresolved import org.python / working with jython and java?
However, I am using ant to compile and run my files. using import org.python.util will give me this error
package org.python.util does not exist
I can see that python.org.util exists in jython-2.5.0.jar.
So, here is the class path I have in my build.xml ant file:
classpath="${java.class.path}:./jgrapht/lib/jgrapht-jdk1.5.jar:\
./jgrapht/lib/jgraph.jar:./jgraphx/lib/jgraphx.jar:\
./jython/lib/jython-2.5.0.jar:./colt/lib/colt.jar:."
and I also I added the path to jython jar files to my class path. i.e. looks like echo $path gives me all the required paths. Is there anything missing here that I am not aware of?
Try this to make all classes in the package available:
import org.python.util.*;
Or this to include a particular class:
import org.python.util.TemplateAntTask;
EDIT: Also, looks like there is an extra slash after jython-2.5.0.jar in your classpath.
Have to create a package defined by me, containing some of the classes, and I recall that package in a file .java created of the program AntlrWorks in which i did the import. Package named "com.project.redfox" . I compiled the code with the command: "javac Test.java provaParser.java provaLexer.java" but I get the error that not exist the package.
In the grammar have added :
grammar prova;
#hader{
import com.project.redfox;
}
....something......
I created the package "com.project.redfox" within of the project redfox developed in NetBeans, therefore the directory com/project/redfox is in the directory redfox.
how can I solve this problem?
To formally answer your question: javac can't find the package com.wikirates which you are probably using in com.project.redfox.
Note that I assumed redfox is a class. If it's a package, you need to import all classes from it like this: import com.project.redfox.*; instead of import com.project.redfox; (assuming that there are classes in com.project.redfox...).