'classpath resource does not exist' for .class file - java

I'm having an issue with a Spring Boot application I'm developing, building using Maven. I'm pretty new to Java coming from C++ & Go I hope this is down to a lack of knowledge than something fundamentally wrong.
I am developing a suite of applications, which all rely on a set of common classes. I have set up 'common' as its own Maven project, albeit without a 'main' class. I am using VS Code and I am able to successfully import, and run in debug, classes from the 'common' project in my application. I have included a Maven dependency in my pom.xml for the 'common' project and in VS Code debug, everything runs fine. I've then run 'mvn package' in both the application and 'common' directories, building a .jar for each under the /target directory (however building 'common' does complain about the lack of a 'main' class but still outputs a .jar - can I specify it as a library rather than an application?)
However when I run the application .jar with the command 'java -jar configservice-0.0.0.1.jar', I'm given the error:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class
[logsystem.configservice.ConfigServiceApplication]; nested exception
is java.io.FileNotFoundException: class path resource
[logsystem/common/lsrestcontroller/LSRestController.class] cannot be
opened because it does not exist
Would someone be able to point me in the right direction on how I could resolve this issue?
Many thanks.
EDIT: Here is the content of ConfigServiceApplication as requested, although quite why this is flagging an issue i'm not sure as the .class isn't referenced in here...
package logsystem.configservice;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#EnableAutoConfiguration
public class ConfigServiceApplication {
//ConfigServiceApplication constructor, called when starting the Spring Boot service
public ConfigServiceApplication( String[] args ) {
System.out.println( "Application Loaded" );
}
}
EDIT2:
package logsystem.configservice;
import logsystem.common.lsrestcontroller.LSRestController;
import logsystem.common.connection.HTTPConnection;
import logsystem.configservice.Config;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#EnableAutoConfiguration
public class ConfigServiceController extends LSRestController {
private Config config;
//Constructor - initialise storage and create cleanup process for it
ConfigServiceController( ) {
super( );
//some more logic specific to this instance
}

Related

Why is the Eclipse IDE giving me "editor does not contain a main type" even though I have a main?

I'm new to Java and in an attempt to make library installation easier, I've decided to use Eclipse IDE. However, my code is not compiling because it says "launch error: Editor does not contain a main type"
Here you can see what the layout of my project folder looks like
It's a beginner's project, so there is a folder called src and inside there is another called ww. Inside this one, there are two files, connectToData.java and Main.java. The connect file contains a structure like this:
package ww;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.json.JSONObject;
public class connectToData {
public connectToData() {
throw new IllegalArgumentException();
}
public static JSONObject getData(String query) {
String u = "https://sky.esa.int/esasky-tap/tap/sync?request=doQuery&lang=ADQL&format=json&query=";
query = "SELECT+TOP+10+,*+FROM+observations.mv_v_v_hst_mmi_observation_optical_fdw_fdw";
URL url;
And my Main.java has a structure like this:
package ww;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, please fix the problems with this code");
They both don't have any other classes or functions inside. Why am I not able to run my code, even though I clearly have a public static void main?
That outlined 'J' means:
This is a java file, but, is not part of any source folder.
Which means you can't run these.
Projects are complex beasts. You don't just have source files - you have a so-called 'source folder'. For example, you could have an application that has test code, a separate installer project, and a plugin for photoshop, in addition to a main app. Obviously, you would want to separate these things out, so that the build tools can for example not include the test code in the plugin, that'd be weird.
As a consequence, projects have so-called 'source folders'. You need at least one, and you can have more than one if you want. In fact, the 'standard model' of what maven projects look like specifically requires that your main app is in src/main/java, for example.
Given that you must have source folders, that also means you can put java files outside of a source folder. And when you do that, you get that J-outline icon as your screenshot shows. As far as eclipse is concerned, these files aren't runnable at all. Perhaps they exist as a template for something (no different from an icon file), or as part of documentation.
The solution is to put these files inside a source folder. The information you provided doesn't let me give you a specific series of steps, but, generally, rightclick on your 'ww' project, find the build settings, add a source folder. Then drag the files you have into the source folder.

unable to view akka http package in IntelliJ

I am trying to use akka http v10.0.2. I added akka-http in my build.gradle file (just akka-http, not http-core or any other akka http related dependency). I tried using the imports below but the 'akka.http.javadsl.server' does not seem available (even though I can see the package when I download the jar file and extract it). The other packages are visible. I have tried clearing the IntelliJ cache. Barring clearing my ivy cache, are there any other steps I can take to troubleshoot this further?
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.Route;
import akka.stream.ActorMaterializer;
import akka.stream.javadsl.Flow;
import java.util.concurrent.CompletionStage;
the issue was resolved by adding all the other akka http dependencies in my gradle build file. I will try to post the build file here when possible.

Package not found, Gradle build

I have the following problem. In my ORMConfiguration file I am setting up some of the basic properties that I need to implement JPA. Inside this ORMConfig.java file, I also have a main class.
The class imports look like this
package io.good.asset.ams.dao.configuration;
import io.good.asset.ams.model.domain.Category;
import io.good.asset.ams.model.domain.Company;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import java.util.Map;
The project is built using gradle and depencies are set in the following manner:
project(':AMSRepository'){
dependencies {
compile project(':AMSModel')
compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
compile("org.hibernate:hibernate-core:5.2.10.Final")
compile("com.h2database:h2:${h2Ver}")
testCompile("org.liquibase:liquibase-core:${liquibaseSpringVer}")
}
}
Since this is a multi-modular project the first two imports in my class are pointing to the different module, and when I try to build that complete project from root, or individual AMSRepository module i get error that
error: package io.good.asset.ams.model.domain does not exist
import io.good.asset.ams.model.domain.Category;
^
error: package io.good.asset.ams.model.domain does not exist
import io.good.asset.ams.model.domain.Company;
My settings.gradle looks like this
rootProject.name = 'AMS'
include 'AMSRepository'
include 'AMSService'
include 'AMSWeb'
include 'AMSModel'
I have also used IntelliJ module settings panel and tried to add manually decencies to this module troublesome module, and the most weird this is that nothing is underlined as wrong and that when I run the main class in ORMConfig.java and instantiate objects from these modules, everything works fine.
Like a war file, a Spring Boot application is not intended to be used
as a dependency. If your application contains classes that you want to
share with other projects, the recommended approach is to move that
code into a separate module. The separate module can then be depended
upon by your application and other projects.
Spring Boot Doc

Can't Import packaged class files from a jar in jython when it contains classes in the default package

I am working on a program that needs to import a jar that has classes both in the default package (root of the jar) and in packages.
So far I have this code and it works so i can import the ones in default but it fails when trying to import ones on packages.
import sys
import os
sys.path.append(os.getcwd() + "/versions/1.7.2.jar")
If I run import a (a is a class as this is a Obfuscated jar.)
it imports but if I run import net.minecraft.server.MinecraftServer it does not work it says No Module named net.
Which I know the class and all packages around it is there so any help?
The problem is that Jython does not correctly find the code.
The source code is based on files like a.class wich you can import but not net.minecraft.server.MinecraftServer.

Spring Web Service Unit Tests: java.lang.IllegalStateExcepton: Failed to load Application Context

I am getting the error "java.lang.IllegalStateExcepton: Failed to load Application Context" when I am trying to run my unit test from within Eclipse.
The Unit Test itself seems very simple:
package com.mycompany.interactive.cs.isales.ispr.ws.productupgrade;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"/applicationContext.xml"})
public class ProductUpgradeTest {
#Test
public void getProductUpgrade () throws Exception
{
//System.out.println(PrintClasspath.getClasspathAsString());
Assert.assertTrue(true);
}
}
But no matter what I seem to do with the #ContextConfiguration I still get the same error.
The applicationContext.xml file resides within a folder /etc/ws, but even if I put a copy in the same folder it is still giving me errors.
I am very new to Java, Spring, Eclipse and Ant, and really don't know where this is going wrong.
The problem is that you use an absolute path in your #ContextConfiguration annotation. You now specify that your applicationContext.xml is located in your root folder of your system (where it probably isn't, since you say it is at /etc/ws). I see two possible solutions:
Use #ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
Move your applicationContext.xml file into your project (for example in your WEB-INF folder) and use #ContextConfiguration(locations={"classpath:applicationContext.xml"})
In case 2, you have to make sure that the directory in which you place your applicationContext.xml is located in your classpath. In Eclipse this can be configured in your project properties.

Categories

Resources