I have some data in my gradle.properties file:
appVersion=1.4.31
appGroup=com.business
appName=projectName
I want to be able to access them in a .java project. I am attempting to access the variable like this:
#Value("${appVersion}")
private int version;
However, when I try to run the application from the main class, I get the following error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
Which shows that it can't find the correct variable.
Attempts at solving the problem
Adding a #propertySource Annotation
I have tried adding this to the main class
#PropertySource(value = "file:build.gradle", ignoreResourceNotFound = true) which was unsuccessful.
Making changes in build.gradle
I attempted the following, taken from this StackOverflow answer:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
Which was also unsuccessful.
This answer below
I did what was said in this answer (I had to add an application.properties file) and I got this error message:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Circular placeholder reference 'appVersion' in property definitions
I thought that it might be getting mixed up with using the same names (appVersion = appVersion), so I changed the application.properties line to
version=${appVersion}
And then in my .java file, I changed the line to:
#Value("${version}")
private String version;
And got the error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
This should work:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
But it seems like you forgot the other part. This just tells Gradle to process your application.properties file and replace any placeholders it finds. You need to also put a placeholder into your application.properties:
version=${appVersion}
Then your value annotation should work (but it should be a String not an int)
#Value("${version}")
private String version;
Finally, since you are launching via IDEA, you may need to make sure that gradle:processResources runs before your application starts so that Gradle has a chance to replace the placeholder you put in application.properties. You should be able to utilize the "Before Launch Options" section in your run configuration to have IDEA run the processResources Gradle Task after the build task. This should cause the placeholder to be properly replaced every time you run the application.
Also, just to help with the understanding. Gradle is the build system for the application, it's not part of the application itself. Once the project is built, everything related to Gradle is irrelevant. This is why you can't just do #Value("${appVersion}") and have it work. The application/Spring doesn't know anything about the tool that's being used to build it (and it shouldn't). So you need to somehow, at build time, inject the Gradle project version into the application if you want access to it. There are a number of ways of accomplishing this but the resource processing method above is a pretty common approach.
Related
Getting
java.lang.NoClassDefFoundError error when running xml file using
testng.
Error details: java.lang.NoClassDefFoundError: Could not
initialize class com.listeners.ExtentTestManager at
com.listeners.Listeners.onTestFailure(Listeners.java:56)
I am using testng version 6.9.10 in pom.xml.
Hi, I am getting this error when I use listeners in my xml file. If I remove listeners and run the xml file, it is working. Please let me know if anything wrong with ExtentTestManager class or the dependencies. I am unable to add code in stackoverflow, so I have attached in this link: selenium99.com/java-lang-noclassdeffounderror Pls check.
You can try maven update project in eclipse after that use clean install, it should work.
Basically
NoClassDefFoundError can occur for multiple reasons like
ClassNotFoundException -- .class not found for that referenced class irrespective of whether it is available at compile time or not(i.e base/child class).
Class file located, but Exception raised while initializing static variables
Class file located, Exception raised while initializing static blocks
But in this case it looks like you are missing some dependency in the building.
you can try cleaning whole project and build again.
If it's a eclipse project
mvn clean eclipse:eclipse
mvn clean package
try above commands
I would like to make my OptaPlanner project run on the module path.
I created an example repository (mwkroening/optaplanner-modulepath-example) based on the cloud balancing demo in the docs.
The example project is running well on the classpath but there is an issue I can't workaround when trying to make it run on the module path. You can see the progress so far in PR #1.
The scoreDrl specified in the solverConfig.xml fails to load with the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: The scoreDrl (io/github/mwkroening/optaplannermodulepathexample/cloudBalancingScoreRules.drl) does not exist as a classpath resource in the classLoader (jdk.internal.loader.ClassLoaders$AppClassLoader#7c16905e).
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:519)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:351)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:255)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:61)
at io.github.mwkroening.optaplannermodulepathexample/io.github.mwkroening.optaplannermodulepathexample.App.main(App.java:14)
I got a similar exception before when calling SolverFactory.createFromXmlResource.
My workaround for that problem was instead calling:
SolverFactory.createFromXmlInputStream(
App.class.getResourceAsStream("cloudBalancingSolverConfig.xml"));
I looked at PLANNER-883 and psiroky/optaplanner-cloudbalancing-jdk9, but I think a classpath resource was used in that case and not one on the module path.
How could I resolve this issue?
Perhaps I should open an issue instead of posting this here, right?
There's an overloaded method of every SolverFactory.createFrom*(...) that accepts a classloader: SolverFactory.createFrom*(..., classloader). That class loader isn't just used for loading the XML resources, but also the DRL resource.
So try:
SolverFactory.createFromXmlResource("/.../cloudBalancingSolverConfig.xml",
App.class.getClassLoader())
I haven't tested this out yet myself, but if it works, I'd love to see this added to the "jigsaw" section in the manual (PR welcome :).
This question already has answers here:
Random NoClassDefFound error in Web Application
(1 answer)
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 6 years ago.
I am trying to run a spring project in intellij.
When I import the code and add the necessary jars, it there is no error during compile time. However, when I run the project, I get the following error
Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess
The KeyValueAccess class is present in the cassandra-access-1.12.0.jar as highlighted in the image.
I have tried to add jars as modules and also as libraries.
What is the correct way to avoid this runtime error?
PS: This is the case for only manually added external libraries. The libraries downloaded with maven work just fine.
Edit
The following code generates the exception
#Bean
public KeyValueAccess keyValueAccess() {
return new CassandraAccessDatastax(); //This doesn't matter, the return type itself creates the exception
}
Exception generated is as below:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.worksap.bootcamp.webeditor.config.WebEditorConfig#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess
I also checked that the jar does it fact have the required class files.
Edit2
I manually ran maven to add the jar files, so I can now find them in ~/.m2/repository. When I call a class in the jar, it seems to find it but when that class calls another class in the same jar, it gives java.lang.NoClassDefFoundError
As I see in your screen, you added cassandra manually, not using maven? So this may be the problem. Add dependencies to pom.xml. Watch them here.
Also check if your dependencies added to special module, not just in libraries. Go to file->project structure->modules. Choose your module in which you use cassandra and click on "dependencies" tab. Press "+" and "library" from dropdown menu and then add your libraries to project.
Hy, I am using Android Studio 0.6.0 as of this morning, during the upgrade process I had to switch to Gradle version 0.11 to compile my Android project.
Ever since I get this annoying error which prevents me from building my project.
Error:Could not find property 'allJava' on source set main.
This property is defined only for the 'java' plugin, but I am using the 'android' plugin in my build.gradle file, so I see no reason why I should receive this error.
Does anyone know how to get rid of this message?
UPDATE:
I solved it, it seems this property is no longer available in Gradle 0.11.1, and somebody somewhere in my project was using it.
I found this line in one of the build.gradle files:
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
which made a reference to another gradle configuration file, that used this property to make a task of generating JavaDocs for every .java file.
So in order to solve this problem, just look everywhere in your code for a reference to this property.
In case anyone happens upon this thread, I did a little investigating into the Gradle Android plugin source.
It appears that the allJava property was removed as it was redundant with the java property. Swapping allJava out for 'java' seems to fix the issue.
https://android.googlesource.com/platform/tools/base/+/5e71a83f74258b85d7f5306bf743c4d69d6242f8%5E%21/#F2
Consequently, the allSource property was also removed, which will break the gradle-mvn-push script as well. I have submitted a bug report for that one, linked below.
https://code.google.com/p/android/issues/detail?id=71174
Is it possible to define an extra property in project A and have it visible in project B? The root projects obviously includes both.
I tried putting this in project A's build.gradle:
ext {
myProps = 'something to say'
}
And this in project B's build.gradle:
task('X', dependsOn: [':A:someTask']){
println(project('A').myProps)
}
but I get:
FAILURE: Build failed with an exception.
...
* What went wrong:
A problem occurred evaluating project ':B'.
> Could not find property 'myProps' on project ':A'.
How can I achieve this?
An extra property is accessible from anywhere the owning object (A's Project object in this case) is accessible from. However, it isn't considered good style to reach out into the project model of a sibling project. One reason is that this can make it necessary to tweak the configuration order of projects, but there are others. Instead, it's better to either declare the extra property in a common parent project, or in a script plugin that gets applied to all projects that need to access the extra property.
PS: In the same vein, an explicit cross-project task dependency should be avoided whenever possible. Also note that your task tries to print the extra property in the configuration phase (rather than the execution phase), which may or may not be what you want.