problems building the protobuf example apps - java

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)

Related

Java project structure best practice in Eclipse

I am using Eclipse to do some Java programming. I am working with an agent-based modelling library, MASON, and GeoMASON which is an extension that adds support for shapefiles etc. I've been having a lot of problems trying to get the included example models to run and I think it's because my file structure is different to what is expected and/or I have the JAR/data files in the wrong locations.
What is the best file/folder setup when working in Java/working with Eclipse and referencing other libraries and running included example models?
My current model, EngD_ABM_MK0, is in the following location with the following supporting files/folders:
/GoogleDrive/ABM/engd/EngD_ABM_MK0/src/engd_abm/EngDModelBuilder.java
/GoogleDrive/ABM/engd/EngD_ABM_MK0/bin/engd_abm/EngDModelBuilder.class
/GoogleDrive/ABM/engd/EngD_ABM_MK0/data/roads.shp
/GoogleDrive/ABM/engd/EngD_ABM_MK0/lib/mason.19.jar
...
The MASON files are in the following location:
/GoogleDrive/ABM/MASON/sim/app/antsforage/AntsForage.java
/GoogleDrive/ABM/MASON/jar/mason.19.jar
...
The GeoMASON files are in the following location:
/GoogleDrive/ABM/geomason/sim/app/geo/cityMigration/CityMigrationModel.java
/GoogleDrive/ABM/geomason/sim/app/geo/cityMigration/cityMigrationData/road_nodes.txt
...
I also have a folder full of all the JAR files I need in the following location:
/GoogleDrive/ABM/lib/geomason.1.5.jar
/GoogleDrive/ABM/lib/mason.19.jar
/GoogleDrive/ABM/lib/j3dcore.jar
...
In Eclipse I have EngD_ABM_MK0, MASON and GeoMASON as Java Projects (see screenshot). The EngD_ABM_MK0 project references MASON and GeoMASON in the 'Projects' Java Build Path and all the necessary JAR files have been added in the 'Libraries' Java Build Path (see screenshot).
Any advice would be appreciated.

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).

How to generate protobuf 3.0 jar java?

I am quiet familiar with protobuf 2.5. I was trying to use protobuf3.0. It seems that the jar is to be generated form the source code available online. But when i import the source code into eclipse and try to create a jar, i can see many errors in the following files
/protobuf3/src/com/google/protobuf/Descriptors.java,
/protobuf3/src/com/google/protobuf/DynamicMessage.java
/protobuf3/src/com/google/protobuf/ExtensionRegistry.java
/protobuf3/src/com/google/protobuf/MessageReflection.java
/protobuf3/src/com/google/protobuf/TextFormat.java
/protobuf3/src/com/google/protobuf/UnsafeUtil.java
This is one of the errors "the import com.google.protobuf.DescriptorProtos cannot be resolved".
Is this the right way to generate the jar (or) is it available anywhere (or) is the full source code available anywhere.
Any help would be appreciated.
Thanks.
This is where i downloaded the source code protobuf3.0-source code
You can find a compiled (JAR) version of Protobuf to download here:
http://search.maven.org/remotecontent?filepath=com/google/protobuf/protobuf-java/3.0.0/protobuf-java-3.0.0.jar
I recommend you look at a build manager such as https://maven.apache.org/ to automatically handle fetching dependencies (libraries) for you.

Can't compile with junit

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!!!

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.

Categories