I have this line in my code:
import org.apache.commons.lang.StringUtils;
I downloaded the Apache Commons Lang 2.6 zip from http://commons.apache.org/proper/commons-lang/download_lang.cgi. There were two choices for Binaries, 1) commons-lang-2.6-bin.tar.gz and 2) commons-lang-2.6-bin.zip. I downloaded the second one and then extracted it. Then I went to the properties for my project and added the commons-lang-2.6 JAR file to my build path (there were two others - javadoc & sources). Then I go to my command prompt and try to do a gradle clean build to see if I'll get a successful build and it says error: org.apache.commons.lang does not exist. Any help to get rid of this error? I'm using myEclipse and it doesn't give me that error, only my cmd does.
If i understand correctly, you added the jar in your eclipse build path. However, this usually doesn't mean you added it to you gradle build script, which means gradle doesn't know about the commons-lang jar file, only eclipse does.
To add it to gradle, you should add it to the build.gradle file, more or less as follows :
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.3.2'
}
Maybe you could try using a gradle plugin for ecplise, like https://github.com/spring-projects/eclipse-integration-gradle/ to automate or make easier to keep ecplise and the gradle script in sync.
Make sure that the classpath for your build script is properly defined.
Related
I have been trying to find a way to BouncyCastleProvider in Java 11 from openjdk. Since there is no ext folder, I can't figure out where to put the jar file. I am using gradle build on MacOS Catalina. It will really help if someone can help me out on this.
I am getting the following error while running gradle build. I have dependency mentioned in gradle as well.
java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
You can use the way you import all your other gradle dependencies.
For example with the dependency:
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.64'
Taken from:
https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on/1.64
(Where you can find other versions if you need them)
If you're unsure how to import gradle dependencies I'd suggest searching Stackoverflow, as I'm sure that been asked many times before.
For example:
How to import external dependencies in gradle?
EDIT: You added another error. Could be the path of the jar needs to be added / classpath.
If you want to add the JAR to your ide, which I guess is Gradle Build Tool if I understood you right. They speak of how to do it here:
How to add local .jar file dependency to build.gradle file?
I want to build a project which has some external libraries. I included pom.xml file inside intellij and it downloaded all of required dependencies files from maven and put them as external libraries, but my project doesn't recognize them and gives compilation error. IDE screen attached below.
this is one of the compilation errors that I get:
Error:(29, 33) java: package com.google.common.collect does not exist
here is the my external libraries which I think are added correctly.
I don't have maven installed but I think IntelliJ handles it by it self.
Have you tried running mvn clean compile either using Intellij or command line ?
Typically when adding a JAR to include with Gradle compilation I do the following:
compile files('lib/sentiment-models.jar')
However, since the JAR I'm trying to include is quite big (200MB) my goal is to instead include it at runtime, similar to how you would add files to the classpath using the java command:
java -cp lib/sentiment-models.jar:. -jar /app/server.jar
So is there a way to use gradle run while including files at runtime, preferably in the build.gradle config file?
Thanks!
Update
I also tried
runtime files('lib/sentiment-models.jar')
but it doesn't seem to work.
I believe that you are using the Java plugin. Please check the documentation, section Dependency management. Try this one:
compileOnly Compile time only dependencies, not used at runtime
This should compile your project and the huge jar should not be included in the distribution. How you will refer it in runtime largely depends on your project type.
I have an external library which is not in any maven repository, but is provided as a jar file. Therefore, I use the following line in the build.gradle:
compile files("/path/lib.jar")
I also have lib-sources.jar and lib-javadoc.jar, which I would like to include into the eclipse project as well, but using them as compile files in the same way as above does not work.
Edit:
For now, I use the approach by Creatorsuperman in combination with manual external source attachment as described here (https://stackoverflow.com/a/8042592). Unfortunately, this does not work with gradle.
I also have lib-sources.jar and lib-javadoc.jar, which I would like to include into the eclipse project as well, but using them as compile files in the same way as above does not work.
Go to project > properties > Build Path > Libraries >Add External Library
I'm trying to use Google protocol buffer for Java(I'm a newbie about Java, just trying).
First of all, I'm using OSX and I've installed protocol buffer with brew install protobuf command.
protoc command works okay.
I've generated MyProtocol.java by protoc.
I've installed protocol buffer for Java as its instruction(README.txt).
mvn install command created .m2 directory and I can find protobuf-java-2.4.1.jar file somewhere in the directory.
I wrote simple test Java code just importing MyProtocol.java and it complains could not find package com.google.protobuf.
So, I've just make jar file mvn package and add its directory as CLASSPATH and it compiled well.
javac -classpath <protobuf-dir>/jara/target/classes Test.java ./com/example/tutorial/AddressBookProtos.java
If I use maven's repository directory (~/.m2/repository), it complains again.
Doesn't maven make CLASSPATH for me if I install the package?
If not, how can I use installed package by maven to compile my code?
The Maven Dependency Plugin has a goal called build-classpath which does what you need. You can get usage hints on the Usage page
Maven doesn't change your setup, so it's up to you to set your CLASSPATH according to your need.
However there are at least two ways in which Maven can halp you:
You can use the Maven Assembly plugin to collect all your dependencies in a directory or an archive, so that you only need to add a single directory to your CLASSPATH in order to run your program;
You can use the Exec Maven plugin to add the running of your program as a goal in your POM and then run it with Maven.