Issue with Spring application, basend on spring.io tutorial - java

I'm trying to learn Spring framework so I decided to follow tutorials available on Spring.io website. I have an issue in my application with the chapter: http://spring.io/guides/gs/rest-service/
I did it with friend day before and app worked and is still working. I tried to do it on my own and in main class I receive error:
Error Stack Trace
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files
The method run(Object, String...) from the type SpringApplication refers to the missing type ConfigurableApplicationContext
I'm trying to do it at my company office right now, can security software in my company's broadband be at fault?
Main class: Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

I think this is related to your class path, which do not have required class files. I suggest you to build project stand alone without any ide to check all dependencies resolution and make sure works fine. For that simply set JAVA_HOME and MAVEN_HOME (assuming you are using build tool is MAVEN)on the environment variable and build your project without any ide (Netbeans or Eclipse) with command (mvn clean compile package install) and it will resolve all your dependencies related problem and download required jars from central repository and on successful build jar will be created in target directory of your project.
And if you are using any IDE then make sure all dependencies are available on the class path.
In your case required jar is spring-boot-starter-web to resolve issue of ConfigurableApplicationContext.

I have resolved my problem by simply right-clicking on my build.gradle file and going into gradle tab, then select refresh. It was fault of compiler, similar issue when people used to develop Android apps on that IDE. Sometimes you have to manually refresh it.

Related

AWS Beanstalk + Kotlin + Spring boot: NoSuchMethodException main()

I've a Spring Boot app written in Kotlin that I have deployed to AWS Beanstalk that is throwing the following exception:
Exception in thread "main" java.lang.NoSuchMethodException: com.prototype.demo.DemoApplication.main([Ljava.lang.String;)
at java.base/java.lang.Class.getDeclaredMethod(Class.java:2675)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
Locally, when I run the app from IntelliJ I've no issues.
I have the spring-boot-maven-plugin and the kotlin-maven-plugin in my project.
Any ideas about what could be causing the issue? I previously built a similar app in Java but didn't run into this issue.
Thank you for your expertise and time.
Update: something interesting to notice is that I had to set in the properties of my POM the following line in order to make the project executable by IntelliJ during development (i had forgotten about it): <start-class>com.prototype.demo.DemoApplication</start-class>
Now I've removed it and I get a different error when running the jar file. I will spend some time on it and see if that's related or not and update the question accordingly.
Thank you!
The issue was raised by the following property declared in my POM file: <start-class>com.prototype.demo.DemoApplication</start-class>.
Once I removed it and added the open keyword to the Configuration annotated classes and to the DemoApplication(the class containing the main) it finally worked.
Solution for build.gradle.kts
I had the same problem when deploying my application to ECS.
First things first:
When you see such an error in AWS logs -> do as the author of the question has done and test the issue locally. Most probably if the application is failing on AWS, it will also fail on you local machine. To do so, you build a jar locally, the jar file is usually saved in build/libs/application-x.x.x-SNAPSHOT.jar and run java -jar application-x.x.x-SNAPSHOT.jar
Now you can fix your build task and test it locally before deploying it again.
Fix Gradle:
My main method was defined in MyApplication.kt file, but in the definition of the main class in gradle I had to set the following:
tasks.getByName<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
mainClass.set("com.company.app.MyApplicationKt")
}
Notice: MyApplicationKt
That is the only thing I had to change for me to fix this issue.

Why are my Alexa Skills Kit dependencies missing?

I cloned Amazon's ASK java repository on github and ran
mvn package
on it, and it produced the following .jars:
ask-sdk-core-2.3.4.jar
ask-sdk-apache-client-2.3.4.jar
ask-sdk-dynamodb-persistence-adapter-2.3.4.jar
ask-sdk-lambda-support-2.3.4.jar
ask-sdk-servlet-support-2.3.4.jar
ask-sdk-2.3.4.jar
I noticed that when attempting to make certain imports for classes on https://developer.amazon.com/docs/custom-skills, such as import com.amazon.speech.speechlet.servlet.SpeechServlet;
I received a message stating that com.amazon.speech could not be resolved, indicating that the file didn't exist. However, after further investigation, I noticed that there was a SkillServlet class that seemed to essentially replace the SpeechServlet class as it was able to do everything that SpeechServlet could,
so I assumed that the developer site hadn't been updated yet to reflect the changes. I then noticed that RequestHandler, a class in the repository, had a method that returned an object of type Optional< Response >. When I tried to import Response with the following import:
import com.amazon.ask.model.Response;
I recieved an error message stating The type com.amazon.ask.model.Response cannot be resolved. It is indirectly referenced from required .class files
This suggests that the class definition doesn't exist in the project's classpath, despite having included all of the above .jars. I searched through and was unable to find a model directory. Did my maven build fail, and am I missing any .jars? I'm using Eclipse EE IDE, which I know is susceptible to errors, but I've cleaned my project as well as restarted the IDE to no avail.
Update:
I noticed that the pom.xml file within the ask-sdk-core-2.3.4 directory contained a dependency for ASK SDK Model, but it doesn't seem to be obtaining the dependency. I also noticed that though ask-sdk-2.3.4 should also include everything that ask-sdk-core-2.3.4 does, as the latter is listed as a dependency in the former, I have to include ask-sdk-core's jar file separately or Eclipse is not able to recognize certain classes. I think this means that maven has failed, so I just downloaded the jar directly from mvnrepository.com. Any idea why this might have happened?
If you use ask-sdk-2.3.4.jar, then it should include all the dependencies you need like you noticed in your update blurb. When you look at Hello World sample in their Github repo, the pom.xml only contains ask-sdk-2.3.4.jar but not other dependencies that ask-sdk-2.3.4.jar has in it. https://github.com/alexa/alexa-skills-kit-sdk-for-java/blob/2.0.x/samples/helloworld/pom.xml
To your error message on Response, that is because you are missing ask-sdk-model jar which you can find from maven central. http://central.maven.org/maven2/com/amazon/alexa/ask-sdk-model/
I would suggest using maven and pom.xml so that you don't have to download jars individually.

Eclipse gradle project setup fake error notification

I want to build a REST Service with Java Spring and followed this guide successfully.
After creating a new gradle project i copied the build.gradle file and started everything with gradle buildRun. It is all woring well, except that Eclipse underlines imports, variable declararations, standard
datatypes as unresolveable.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
For example are in this class String, SpringApplication, SpringBootApplication, Application and org.springframework marked as unresolvable. However Exlipse resolves them just fine. I hope someone can
tell me what i have to change, error detection is quite hard right now (:
The platform is:
Eclipse IDE for Java Developers
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200
OS: Linux, v.4.16.13-2-ARCH, x86_64 / gtk 3.22.30, WebKit 2.20.3
I had this issue before with Spring (although it was with with Maven) and I was able to resolve it by updating my Maven project and running a clean build. Check and see if it possible to "refresh" your gradle build/update dependencies:(For me it was Right-click -> Maven -> Update Project), and also try running gradle clean first, then build it to see if leftover artifacts are causing the issue.
If you get a "Could not find or load main class" or an error like that from Spring later, make sure your directory structure (from the project) is src/main/java/hello, with hello being the package containing your class files.

Testing Java code with Groovy under Intellij: unable to resolve class GroovyTestCase

I need to write a short test for some Java code. I used CTRL+SHIFT+T to generate one with IntelliJ, and selected "Groovy JUnit" as the testing library, then wrote the following test:
package util
class FibonacciHeapTest extends GroovyTestCase {
FibonacciHeap<Integer> heap
void setUp() {
super.setUp()
heap = new FibonacciHeap<>()
}
void testAddInOrder() {
testForItems 1..1000
}
private void testForItems(Range<Integer> items) {
items.each {heap << it}
assertEquals heap.size, items.to
items.each {assertEquals heap.remove(), it}
}
}
However, when I right click on the test case in the project window, I don't get the "Run All Tests" option that I normally do with JUnit tests, and the compiler throws the following error:
Information:2/4/15 8:15 PM - Compilation completed with 2 errors and 0 warnings in 2 sec
/home/patrick/IdeaProjects/hackerrank/src/test/java/util/FibonacciHeapTest.groovy
Error:(3, 1) Groovyc: unable to resolve class util.FibonacciHeap
Error:(9, 1) Groovyc: unable to resolve class GroovyTestCase
Trying to import GroovyTestCase or FibonacciHeap manually causes the same error. IntelliJ does not add any import statements when I let autocomplete finish the names for me, like it usually would with Java code.
What am I doing wrong?
This worked for me :
Open Gradle window (on right side in my case)
Click on refresh button
Done
I had a similar problem with creating test classes in IntelliJ, and it was solved when creating a new directory outside of the com.company folder (where I had the class I wanted to test).
Create a new directory for the test classes on the same level as your src folder
Right click on your new test directory, and "Mark directory as" --> "Test Resources Root"
Now create a test class, which should automatically be added to your test directory.
Build -> Rebuild project in the IDE itself (as opposed to maven, in my case) did it for me.
In my case, what I did to resolve the issue was rather simple.
Close IntelliJ
Open the attached homepage...
Remove your project by clicking on the x then...
Click on Import Project, Navigate to the build.graddle file of your project and open.
That was it and all the Red highlightings disappeared.
maybe you need add groovy-all rather then groovy,such as :
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
You have to configure Groovy SDK first. See the screenshot
More detailed description in the official document: Configuring Global, Project and Module SDKs
As #sman591 pointed out in a comment, if you are getting the error:
groovyc: unable to resolve class groovy.util.GroovyTestCase
and you already have groovy as a dependency then you are probably just missing the junit dependency.
In IntelliJ IDEA I re-imported the project. It worked then.
I closed idea. I removed .idea folder in the project. And I imported the project.
Then I needed to set up Groovy, see previous answers, mark test directory as test source in all modules of my project.
I was selecting the root folder which had build.gradle file in it, but it didn't work.
Steps followed are similar to #Pila
close the project from intellij
remove .idea, log, out folders
go to intellij dashboard
import project from existing resources
select build.gradle file <-- this is important
now you should see all the tasks are getting build in the background, and once build is finished all red lines are gone.

JUnit error: Could not find or load main class (RemoteTestRunner)

I've downloaded Junit version 4.10 and loaded the jar via the build path but I seem to get this error message when running a Junit test in Eclipse:
Error: Could not find or load main class org.eclipse.jdt.internal.junit.runner.RemoteTestRunner
My Test class is the following:
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class InternetConnectTest {
#Test
public void testConnectMethod(){
InternetConnect net = new InternetConnect("www.google.com");
assertEquals("Result", "www.google.com", net.url);
}
}
I came across this after I updated java versions: "junit: could not load main class 1.1". Go into the eclipse external tools configurations, open the entry for the ant file you are running, then go to the JRE tab, and select "Separate JRE", also making sure your new jre is correctly listed in the Installed JREs...for me, this took care of it.
This is an Eclipse error message. It seems your installation is corrupted. Try using a fresh installation of Eclipse.
Before trying a fresh install i just restarted eclipse and it did the trick.
I just had the same problem.
I relaunch Eclipse with the "-clean" option, and it did the trick too.
Edit:
How to run eclipse in clean mode? and what happens if we do so?

Categories

Resources