java.lang.NoClassDefFoundError bei importing maven Project to another one - java

I have 2 Projects in Eclipse:
The first one is a maven project using google AdWords API dependencies and the second one is a dynamic web project. I have imported the first project by clicking on the second -> import -> Existing Maven Projects -> MyFirstProjectFolder -> Finish. It made a new pom.xml.
I've used a class from the first project in the second by importing it and running a method of it and i get an error: java.lang.NoClassDefFoundError.
I'm not a professional java programmer but as I understood the second project doesn't get the class but in my eclipse editor I have no errors or mistakes.
I hope I was precise enough. Thank you in advance.
package com.adwordsfeatures;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import url_checker.*;
#Path("/hello")
public class ServerService {
#GET
#Produces(MediaType.TEXT_PLAIN)
#Path("first")
public String hello(){
return "Hi everyone";
}
#GET
#Path("go")
public String go(){
runProgramMethod();
return "<p1>Programm running</p1>";
}
private void runProgramMethod() {
MainPauserMethod runner = new MainPauserMethod();
}
}
this is the code in the second Project and the class i'm trying to use from the first project is the MainPauserMethod (i know it's a class it's just a test i'll change the name) and it's from the package url_checker.

Related

How can I call methods from a different java project?

It's as the title says. I'm trying to call methods defined in another Java project. Is there a way I can do that? I tried import statements but that didn't work.
EDIT:
So here is what is sitting in the code now in terms of imports:
enter image description here
and here are some of the functions I want to call that are in the other project:
enter image description here
What I've tried is:
import com.example.cs320EthicsPlayer.api.*
but that doesn't work it just says import can't be resolved.
Where the 2 projects are located:
enter image description here
I'm not too familiar with mvn directories but we are using maven. The methods I want to call are from the cs320EthicsPlayer folder (project) and the file I'm calling it from is from partyinthebackend (another project). I haven't called on the other project at all, and that's what I'm trying to figure out.
Class Path for the file I'm trying to call the functions from:
enter image description here
Let's say we have a class Test inside a package com.example :
package com.example;
public class Test {
public static String getHalloWorld (){
return "Hello world";
}
}
All we need if we want to use our class Test in another package is to use import like that
import com.example.Test;
class OtherPackage {
public static void main(String[] args) {
String geeting = Test.getHalloWorld();
System.out.println(geeting);
}
}
You should remember anything you want to use in another package it should be public.
So just check where is the method, which package and class include the method you are trying to import.
Let's fix your problem now:
try
import com.example*
Now you import the whole package, but you should remember you can import and use just the public method from the package example.
Update:
I see you have updated your question again, and you want to use maven, I think that will answer your question :
Java project dependency on another project
I hope that answers your question.
If it is in the same project but different package you can just doing
import package name...
If it is a complete different project you can't import them. You need to re-insert the methods.

where should I put RestController in spring boot project

I used idea IDE to developed a Java springboot project follow a tutorial.
However, in the tutorial, there's a function shown below. I do not know where to put this function in me spring boot project.
The function:
package com.helloworld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by fangxiao on 2017/3/24.
*/
#RestController
public class Controller {
#RequestMapping("/helloworld")
public String helloWorld() {
return "helloworld";
}
}
where should I put this function as for the below project structure?
project structure
Following your example, the Controller Class have a organization like this picture:
I also created another packages that are frequentelly used in a spring-boot project
Since your package is :
package com.helloworld.controller;
You can create folder structure inside java folder com -> helloworld -> controller and keep the code (Controller.java class) inside.
Create a package under src/main/java like com.abc.rest and create RestController class
:Here is the sample code and to access this api use url like : localhost:8080/test/headers
#RestController
#RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Rest {
#GetMapping("/headers")
public String listAllHeaders(#RequestHeader Map<String, String> headers) {
headers.forEach((key, value) -> {
System.out.println(String.format("Header '%s' = %s", key, value));
});
return "success";
}
}
For learning purpose is good to do it manually, but if you want to skip it, you can create the class shown above in the src/main/java and IntelliJ will dectect and offer to relocate it to the proper package.
Create your package com.project.controller under
D:\Bill\java\test\src\main\java
directory

Jax-ws endpoint not asking to create wrapper classes

I'm following this simple tutorial: https://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/?fbclid=IwAR0vxhYrj9MKy1Q28h6luFVJoSxDP4KWBOLEu_v_Ss4uQztmB-9JuYsS4RI and at step 3 it mentions that I should receive the error:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?
However, I do not get such error(no error at all) and I'm worried that it is not working as expected.
My classes:
Interface:
package com.soap3sk.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
// Service Endpoint Interface
#WebService
#SOAPBinding(style= Style.DOCUMENT, use= Use.LITERAL) // optional
public interface NoteEndpoint {
//#WebMethod ArrayList<ToDoNote> getNotes();
#WebMethod String getHelloWorldAsString(String name);
}
Implementation:
package com.soap3sk.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.soap3sk.ws.NoteEndpoint")
public class NoteEndpointImpl implements NoteEndpoint {
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Publisher:
package com.soap3sk.endpoint;
import javax.xml.ws.Endpoint;
import com.soap3sk.ws.NoteEndpointImpl;
public class NoteEndpointPublisher {
public static void main (String[] args) {
Endpoint.publish("http://localhost:5000/ws/hello", new NoteEndpointImpl());
}
}
Project structure: https://imagizer.imageshack.com/img924/3514/BAuOcl.png
What I also noticed that those 2 .class files(asString and Response that are mentioned in the guide) are not generated anywhere as well. I'm using Eclipse and created a maven project with the quickstart archetype. Runnning it as a standard java application.
I can access the wsdl file going here: http://localhost:5000/ws/hello?wsdl and the I can see getHelloWorldAsString and getHelloWorldAsStringResponse there, but they are nowhere to be seen in my project and no error is thrown that they could not be found as mentioned in the guide that it should.
I also tried downloading the sample project and deleting the .java files that should be required, but it is stil the same(no error, not asking to create those classes).
I would be very grateful if someone could help. Thank you.
EDIT
I found a similiar question here: Java web service not giving error message Could someone explain his answer? Is the creation of those two classes not necessary?
you're trying to replicate a situation reported almost 10 years ago. Don't you want to try a newer tutorial like the following:
https://www.baeldung.com/jax-ws
https://spring.io/guides/gs/producing-web-service/

Run feature file with cucumber java

Im trying to run a simple feature file with java and intellij and I cant seem to get it working.
Ive set up my Cukes test runner class
package config;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(monochrome = true,
features = {"src/test/resources/features/"},
dryRun = false,
glue = "com.ecs.googleuat"
)
public class RunCukesTest {
}
feature:
Feature: home
Scenario: home
Given I am on the home page
step definitions:
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
public class MyStepdefs {
#Given("^I am on the home page$")
public void iAmOnTheHomePage() {
System.out.println("Hello");
}
}
project structure:
Im using a maven project with the java cucumber plugins.
When I run the feature file I get the following error:
Undefined step: Given I am on the home page
1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.000s
You can implement missing steps with the snippets below:
#Given("^I am on the home page$")
public void i_am_on_the_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 0
Please help me understand what Im doing wrong.
Thanks
Not 100% sure but I think the issue is with the method name. The easy solution just copy-paste the suggested step definition inside MyStepdefs and remove exception and add your println and then run. Also try removing the glue com.ecs.googleuat you added.
Please follow this for further help!
Though I would strictly recommend going ahead with java-8.
Anyways, If you are comfortable with Java-8. Remove the glue inRunCukesTest.java. And update your MyStepdefs.java
public class MyStepdefs implements En {
public Stepdefs() {
Given("^I am on the home page$", () -> {
System.out.println("Hello");
});
}
}
En automatically implements default Glue for you.
Also, use appropriate dependencies. Follow this for same.
Edit:
For Java 8 please make sure that if you are using IntelliJ Idea, then Project SDK is enabled to 1.8.

Simplest way to unit test an android library application?

Sorry if this is a bit of a vague question, however im struggling to find a single solid example on how to do unit testing (isolated testing) with Android...
Here is an example of what i'm wanting to achieve:
// Some class
class Calculator
{
public int Add(int a, int b) { return a+b; }
}
// Simple test
import org.junit.Assert;
import org.junit.Test;
class CalculatorTests
{
#Test
public void should_add_numbers_correctly()
{
Calculator calculator = new Calculator();
int expectedResult = 5 + 5;
int actualResult = calculator.Add(5,5);
Assert.assertEqual(actualResult, expectedResult);
}
}
So one project contains models and logic, then another project contains tests for said library. There is no front end or UI, so I want to do the bare minimum to just be able to test that my methods all work in isolation.
As long as your "library" doesn't contain any references to resources in the Android SDK, there isn't anything more to this than regular unit testing. In your Eclipse workspace, say you have your main project MyAndroidLibProject, you simply create a new Java Project (e.g. MyAndroidLibProjectUnitTests). Inside here, you create ordinary unit tests referring to the Calculator class in your main project (just make sure that your main project is added to the build path of your test project).
You might also find some additional information in a similar question I asked myself earlier, as well as the answer to that question.
Updated with example:
import static org.junit.Assert.*;
import org.junit.Test;
public class SemesterTest
{
#Test
public void aNewSemesterShouldHaveANegativeId()
{
Semester semester = new Semester(2010, SemesterType.FALL);
assertEquals(-1, semester.getInternalId());
}
#Test
public void toStringShouldPrintSemesterTypeAndYear()
{
Semester semester = new Semester(2010, SemesterType.FALL);
assertEquals(SemesterType.FALL + " 2010", semester.toString());
}
#Test
public void equalityShouldOnlyDependOnSemesterTypeAndYear()
{
Semester aSemester = new Semester(2010, SemesterType.FALL);
aSemester.setInternalId(1);
Semester anotherSemester = new Semester(2010, SemesterType.FALL);
anotherSemester.setInternalId(2);
assertEquals(aSemester, anotherSemester);
}
}
The above is a test of my own Semester class (a simple data class representing a semester). Semester is located inside my android project MyMainProject (but the class itself doesn't contain any references to the Android SDK). SemesterTest is located inside my test project MyTestProject (an ordinary Java project), with both MyMainProject and MyTestProject being in the same workspace (and since SemesterTest has the same package name as Semester, I don't need any special import statement to reference Semester either). MyTestProject also has MyMainProject added to its build path (junit.jar is also added to the build path, but this happens automatically, at least in Eclipse I think).
So as you can see, this is nothing but a completely ordinary unit test (JUnit 4, just to have mentioned it). Hope this helps.

Categories