Can't compile with junit - java

This is a seemingly easy exercise that I can't get to work. Ultimately, I need to get junit running with ant, but for now I'm just trying to compile a very basic junit test from the terminal (OSX 10.10.5) using javac (I've gotten this exercise to work in eclipse; I'm aware that you have to install junit differently depending on if you're gonna use it in Eclipse or the terminal, and I think that I did that correctly, but probably not).
Here's a link to the example. I'm running Java version 1.8.0_25
I downloaded the two .jar files for junit and placed them in the classpath, but I'm getting multiple compiler errors related to not being able to find org.junit...
TestJunit.java:1: error: package org.junit does not exist
...
TestRunner.java:1: error: package org.junit.runner does not exist
The java files from the example (TestJunit.java and TestRunner.java) are in ~/JUNIT_WORKSPACE.
This may be TMI, but here is the entirety of my ~/.bash_profile. I have a feeling that this is where I'm messing up.
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home"
export JUNIT_HOME="/usr/local/junit"
export ANT_HOME="~/apache-ant-1.9.6/"
export PATH="$PATH:$JUNIT_HOME"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit4.12.jar"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/hamcrest-core-1.3.jar"
export PATH=$PATH:$ANT_HOME/bin
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
alias l='ls -FAlh'
I have restarted the terminal (and/or entered source ~/.bash_profile) and still can't get this to work. Please help!!!

Related

I keep getting "error package com.mongodb does not exist" when trying to compile MongoDB Utilites class

I am running a webapp using a connection to MongoDB where products reviews are stored. The current version of the webapp works correctly. (i.e. it writes and stores new reviews in the MongoDB collection).
Nonetheless, when I make changes and try to compile a new version of the MongoDB Utilities class I keep getting:
error package com.mongodb.XXX does not exist
import com.mongodb.BasicDBObject;
^
I do have the following .jar files in the \lib directory:
mongodb-driver-3.6.3
mongodb-driver-core-3.6.3
mongodb-java-driver-3.6.3
bson-3.6.3
and I mentioned them in the CLASSPATH variable:
set CLASSPATH=.;C:\apache-tomcat-7.0.34\lib\servlet-api.jar;C:\apache-tomcat-7.0.34\lib\jsp-api.jar;C:\apache-tomcat-7.0.34\lib\el-api.jar;C:\apache-tomcat-7.0.34\lib\commons-beanutils-1.8.3.jar; C:\apache-tomcat-7.0.34\lib\mongo-java-driver-3.6.3.jar; C:\apache-tomcat-7.0.34\lib\bson-3.6.3;C:\apache-tomcat-7.0.34\lib\mongodb-driver-3.6.3; C:\apache-tomcat-7.0.34\lib\mongodb-driver-core-3.6.3; C:\apache-tomcat-7.0.34\lib\mysql-connector-java-5.1.38-bin.jar;
What am I doing wrong? How can I get java to compile my new MongoDB Utilities class?
I couldn't reproduce the same behaviour, but I only reference mongodb-java-driver-3.6.3 in my build scripts.
Since the mongodb-java-driver is an uber JAR that contains mongodb-driver, mongodb-driver-core, and bson, you could try removing these latter three from your classpath and build scripts and see if that resolves the issue.
I was able to solve my issue so I post this answer in case someone is stuck in the same problem.
Thanks to what user "nos" posts in the answer to this question I used the -verbose option when compiling e.g.:
javac -verbose className.java
In the errors log I noticed that the Java compiler was searching for the MongoDB classes in a different \lib folder than the one I used in my CLASSPATH definition. So I added a copy of the mongodb-java-driver there and the compilation worked.
As craigcaulfield correctly mentions above there is no need to add the other drivers ( mongodb-driver, mongodb-driver-core, and bson).

Java not processing JUnit jar

I am unable to compile tests with JUnit. When I attempt to do so, I get this error:
package org.junit.jupiter.api does not exist
I get this error compiling the tests even if I put the .jar in the same directory and compile as follows:
javac -cp junit4-4.12.jar Tests.java
The contents of Test.java are:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class Tests {
... several tests ...
It's not clear to me what the issue is, and as far as I can tell, it should work with the .jar -- it's the one from /usr/share/java, where it was installed when I installed junit.
As #DwB has already mentioned you have wrong junit version.
Here is what is jupiter in JUnit: http://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5
In simple words JUnit Jupiter API is a set of new classes which were written and introduced in junit 5 version only. And ur trying to use 4 version.
And also i want to clarify some points.
even if I put the .jar in the same directory and compile as follows
It does not matter actually is your file in the same directory or not. Its all about it's path. If you are setting jar only by name of jar file (as you did) then your path becomes relative to your current directory from where u execute javac command. You can just use absolute path and run this command from every directory you want.
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html (this one is for windows but for other os there are only minor changes in path writing)
If you get errors like package does not exist, classnotfound or anything similar then such kinds of errors almost always mean you have something wrong with your classpath or dependencies. In your case you simply had wrong version.
Now about finding necessary deps. In java world one of the main places for dependencies is maven central. Almost every opensource library can be found there and maven by default uses this repository to find and load dependencies (in your case these are jars) from there. Also you can use it to get necessary jars manually by simply using it's UI (https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0). There is download jar button.
Now if you know package or class but do not know in what dependency (jar for simplicity) it is located. In this case you can use http://grepcode.com or other resources which allow to search within available source code withit different repositories. In most cases this work. With juniper i did not manage to find smth there but in other cases this may help) Or the most simple case is just google package and in most cases it also will help to define entry point.
Now about solving ur issue. It seems that you will need as api as implentation. You will definitely need this one https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0 but it seems that you will need juniper-engine too. First try adding only API and then just go on adding necessary libraries according to errors. You can add multiple jars to cp (read provided class path guide from oracle).

Java files are not compliling

I have deploy my java code on AWS cloud, when I compile it on windows through terminal, I just have to use the command.
javac mainApp.java
it automatically create .class files of all other classes, lets say I have another class
class newProcessClass
who's variable is in mainApp.java, on EC2 when I compile it, it is giving me the error
error: could not find this symbol
newProcessClass npc = new newProcessClass();
same for the other classes. how can I compile it, and run it.
According to your description, I think that might be caused by classpath issue,
So please make sure current path(.) is under your classpath.
You can do this by type the follow into your terminal(pay attention to the little dot please!)
export CLASSPATH=.;$CLASSPATH
If you would like to persistent this setting and avoid set this everytime, you'd better add it to your .bashrc file.
And then when you run the class via java command, please also specify classpath as below
java -cp . mainApp
I recommend use Maven for the life cycle for application java and when you compiled the mainApp.java this action has that compile all file. you can get me more information of error stack?. For other side I think that is best that you use Maven. Best regards

Getting ClassNotFound Exception for class MkActivityMethod

I am trying to use JackRabbit lbrary for SVN chekin operation.
What I run my code through individual program this works fine. But when I run using web based project it doesn't work.
Code compiles fine but gives me runtime exception at following line :
MkActivityMethod activityMethod = new MkActivityMethod(url);
The exception is :
java.lang.NoClassDefFoundError: org/apache/jackrabbit/webdav/client/methods/MkActivityMethod
My project has jackrabbit-standalone-2.6.4.jar included in my eclipse jars as well as in project web-Inf/lib folder
Please let me know where I am going wrong.
There is obviously something wrong with your classpath. What Web Server Are You Using?
Here is my solution :
Try to build the project once again
Check the JDK version for the builder and Server JRE
Try clean the project (In eclipse , Project menu -> Clean )
Reason :
After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. It probably indicates that you haven't set the classpath option when executing your code. This link explains how to set the classpath when you execute.

problems building the protobuf example apps

I'm new to protobufs and was trying to learn more about using them. I've downloaded the protobuf packaged from here. There is a README.txt file inside the examples folder of the archive which gives instructions on how to build 2 example applications. However when I follow those instructions for building the java application:
make java
I get a lot of errors followed by:
100 errors
make: * [javac_middleman] Error 1
All of the 100 errors seem to be classpath related, as this is a typical example:
com/example/tutorial/AddressBookProtos.java:37: error: package com.google.protobuf does not exist
Any ideas about how to get passed this?
The problem is that for some reason protobuf jar is not added to the classpath during compilation. To fix it you should open examples/Makefile and add -cp protobuf-java-2.4.1.jar at the end of java complilation line javac AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java.
P.S. If you built you protobufs with maven the jar is located at ~/.m2/repository/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar (version of the jar might be different)

Categories