#Re-open java gradle resource

76 messages · Page 1 of 1 (latest)

broken field
#

I'm still here for the same problem of few days ago, using InputStream s = getResourceAsStream("/nome.png") it keep saying s is null, my resource folder is src/main/resources

karmic cometBOT
#

This post has been reserved for your question.

Hey @broken field! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

broken field
#
InputStream s = ResourceFuncUtilities.class.getResourceAsStream("/" + fileName +".png");
lethal pollen
broken field
#

how can I check it?

lethal pollen
#

what ide do you use?

broken field
#

vs code

lethal pollen
#

oh sorry then, I don't know specifics of vs code

broken field
#

btw I understand what u mean

#

In the compiled classes there aren't resources files

lethal pollen
#

ide usually has a directory marked as resource directory. And during compilation it copies all the files from that directory to place with the binary files

broken field
#

Yes that's my problem, if I copy them manually obv it works

lethal pollen
#
"assets/*": [
        "./assets/*" // <-- relative to baseURL, so it will resolve to ./src/assets/*
      ]
``` Found this setting to set assets folder. Did you try using it?
broken field
#

if I right click on resources folder there's an option "Add folder to java source path" but when I click it says please use build.gradle file to manage the sources directories, then even if I'm searching from this morning I can't understand how to add in it.

lethal pollen
#

I don't think that you should make it as source folder. Is there anything like "make it as resource folder"?

#

also, give a try to add 'assests` settings into gradle

#

I took it from here

broken field
#

I tried many ways to add the foulder into gradle

#

but it still keep saying s is null

#
public static BufferedImage loadSources(final String fileName){
        BufferedImage img = null;
        InputStream s = ResourceFuncUtilities.class.getResourceAsStream(fileName + ".png");
        try {
            img = ImageIO.read(s);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return img;
    }

this is my full func

lethal pollen
#

I strongly recommend stop using vs code with java. You should use intellij or eclipse

broken field
#

surely ur right but this one I'll continue with vs

#

btw my structure is
src/main/java
src/main/resources
this should be correct, I mean, resources should be the default resources dir

honest fossil
broken field
#

ok

#

into build/resources there are all the files

honest fossil
#

send your buildscript if possible

broken field
#

all the images

honest fossil
#

and how are you running it? using the gradle application plugin?

broken field
#

if that is the "play triangle" on top right yes

honest fossil
#

send buildscript

broken field
#

file build.gradle.kts?

honest fossil
#

yes

broken field
#

ok

#

I send with this

honest fossil
#

yes

#

send it inside a
```kt
```
block

#

the "kt" part adds syntax highlighting to make it readable for me

#

or just send it as a file

broken field
#
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.3/userguide/building_java_projects.html
 */

plugins {

    // Apply the java plugin to add support for Java
    java

    // Apply the application plugin to add support for building a CLI application
    // You can run your app via task "run": ./gradlew run
    application

    /*
     * Adds tasks to export a runnable jar.
     * In order to create it, launch the "shadowJar" task.
     * The runnable jar will be found in build/libs/projectname-all.jar
     */

}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    
    // Suppressions for SpotBugs
    compileOnly("com.github.spotbugs:spotbugs-annotations:4.7.3")

    val jUnitVersion = "5.9.1"
    // JUnit API and testing engine
    testImplementation("org.junit.jupiter:junit-jupiter-api:$jUnitVersion")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$jUnitVersion")

    // This dependency is used by the application.
    implementation("com.google.guava:guava:30.1.1-jre")
}

application {
    // Define the main class for the application.
    mainClass.set("i.u.dk")
}

spotbugs {
    omitVisitors.set(listOf("FindReturnRef"))
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}
honest fossil
#

your main class is called i.u.dk?

broken field
#

it is the package

#

i/u/dk/myclass.java

honest fossil
#

then you're not running it via the gradle run task

#

the class itself is named myclass, no capitals?

broken field
#

I wanted to kinda censor the package

honest fossil
#

ah

broken field
#

myclass i mean my class name

honest fossil
#

alright, so put i.u.dk.myclass (replace as appropriate) for your main class

#

linux or windows?

broken field
#

windows

honest fossil
#

run it by doing gradlew.bat run in a terminal

broken field
#

ok

#

ok

#

did it

honest fossil
#

does it work?

broken field
#

yeah

#

it works

honest fossil
#

perfect

broken field
#

if it's not a problem can u explain me?

honest fossil
#

I recommend either installing a vscode gradle plugin

#

or using intellij

broken field
#

is it good from vs code extension?

honest fossil
honest fossil
broken field
#

I have already installed "Gradle for Java" from microsoft

honest fossil
#

ah then that's what you need

#

1 sec

broken field
#

btw I can run just with gradlew run isntead of gradlew.bat run

honest fossil
#

yeah that works too

#

here's how to run it in the IDE:

Tasks can be run via:

  • Gradle Projects or Recent Tasks tree views
  • Run Task command
  • Run a Gradle Build command

A running task will be shown with an animated "spinner" icon in the tree views, along with Cancel Task & Restart Task buttons. The Cancel Task button will gracefully cancel the task. The Restart Task button will first cancel the task, then restart it.
Gradle Tasks Running

A task will be run a vscode terminal where you can view the task output.

Send a SIGINT signal (ctrl/cmd + c) in the terminal to gracefully cancel it.

Tasks run via the Run a Gradle Build command are not reflected in any of the tree views. Use this command to specify your own Gradle build arguments, for example to run multiple tasks or to exclude tasks.

broken field
#

You really saved my life today

#

I'll start using gradlew run to run project

#

in fact having gradle why run it from the ide triangle when I can use gradlew run