# ND4J Error: class path resource \[\] cannot be resolved to URL because it does not exist

**URL:** <https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890>\
**Category:** ND4J\
**Created:** [September 29, 2020, 4:30am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890 "2020-09-29T04:30:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 4:30am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/1 "2020-09-29T04:30:03Z")

</div>

{Moving the issue from GitHub to here. Please find the original post [here](https://github.com/eclipse/deeplearning4j/issues/9100)}

#### Issue Description

I am trying to use .npy files in my Kotlin code, but when I try to load it with this code (as given in tests and example codes):

```auto
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.indexing.NDArrayIndex
import org.nd4j.linalg.io.ClassPathResource
import java.io.File

fun main() {
    val homePath = System.getProperty("user.dir")
    println("Working Directory = $homePath")

    val fileName = "src/assets/Feedback.npy"
    val file = File("/$homePath/$fileName")
    println("File size = " + file.length())

    val validFile = ClassPathResource("/$homePath/$fileName").file
    println(validFile)
}

```

I am getting this error:

```auto
Working Directory = /Users/vidhey/coding/Kotlin/intellij-sandbox
File size = 11691
Exception in thread "main" java.io.FileNotFoundException: class path resource [Users/vidhey/coding/Kotlin/intellij-sandbox/src/assets/Feedback.npy] cannot be resolved to URL because it does not exist
	at org.nd4j.linalg.io.ClassPathResource.getURL(ClassPathResource.java:269)
	at org.nd4j.linalg.io.AbstractResource.getURI(AbstractResource.java:57)
	at TestKt.main(Test.kt:14)
	at TestKt.main(Test.kt)

Process finished with exit code 1

```

I am not sure if I’m using the code wrong or if this is some bug.  
Though I noticed one thing: the path starts with “Users/vidhey/…” instead of how it should on an absolute path “/Users/vidhey/…”, even though I set the right working directory and passed the proper path.

#### Version Information

- ND4J-native 1.0.0-beta6
- macOS Catalina on MacBook Air 2015
- Working on IntelliJ IDEA 2020.2.2 Ultimate

I tried using beta7 and beta6 both with the ClassPathResource and with the File object.  
With the ClassPathResource, I’m getting the error I showed above. With the File object, the app crashes with some kind of segmentation fault:

```auto
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000012eaf7614, pid=3819, tid=0x0000000000000c03
#
# JRE version: Java(TM) SE Runtime Environment (8.0_162-b12) (build 1.8.0_162-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.162-b12 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C [libnd4jcpu.dylib+0x32614] lengthForShapeBufferPointer+0x4
#
# Core dump written. Default location: /cores/core or core.3819
#
# An error report file with more information is saved as:
# /Users/vidhey/coding/Kotlin/intellij-sandbox/hs_err_pid3819.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

```

I have posted the entire core dump log on the [issue I referenced above](https://github.com/eclipse/deeplearning4j/issues/9100) if you want to check it out.

I have been using the same files in Python without any issue, so the files are not corrupted. But I am setting `pickle=True` in Python when making these files since these are quite big files. Can that be the issue?

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 4:39am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/2 "2020-09-29T04:39:23Z")

</div>

Here’s the sample code I’m using to build my npy files in Python. For context: df is a DataFrame I open from a CSV

```auto
a = {(df["col1"][i],df["col2"][i],df["col3"][i]) : (df["col4"][i], df["col5"][i]) for i in range(123)}
np.save("path/to/dir/my_file.npy", a)

```

I say files because I use 4-5 different NPY files in my code.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 29, 2020, 4:41am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/3 "2020-09-29T04:41:27Z")

</div>

@vidheyoza you mentioned in your github issue you were using pickle. Could you make sure that’s not the case for all np.save calls and let me know how it goes?

We use the binary numpy format not pickle for loading ndarrays.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 4:43am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/4 "2020-09-29T04:43:12Z")

</div>

So I don’t use `pickle=True` while saving any of the files, but I still need to do that when I load them elsewhere in other Python code. Is `pickle=True` a default setting?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 29, 2020, 4:48am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/5 "2020-09-29T04:48:11Z")

</div>

@vidheyoza could clarify why you need pickle=True? You say you need that but don’t’ necessarily specify why. np.load/save calls should just be consistent if you use them. I’m not sure if it’s the default or not, I’d just look at the docs for np.save to see: [numpy.save — NumPy v1.23 Manual](https://numpy.org/doc/stable/reference/generated/numpy.save.html)  
It appears to be true yes.  
I’d suggest shrinking your tests down.

I would advise for any debugging you ever to do to always narrow down the problem, especially so others can reproduce it. When working with interop with any language (eg: python to java) or anything like that always try to understand what each language expects and what the defaults for serialization are.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 4:51am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/6 "2020-09-29T04:51:54Z")

</div>

Right I seem to be getting closer to the issue. np raises an error saying `ValueError: Object arrays cannot be saved when allow_pickle=False`. Looks like I cannot save my dict based arrays in binary files, and I cannot use pickled files in ND4J.

Thanks for helping me narrow down the problem! I’m new to ND4J so I didn’t know that the library doesn’t support pickled files. Do you have an idea of how I could save dict arrays so that they can be read by ND4J?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 29, 2020, 4:57am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/7 "2020-09-29T04:57:04Z")

</div>

Sure no problem! If you need to use dicts, then I would advise separating that out. In nd4j this would be an:

Map\<String,INDArray\>

You could do a zip file instead. I know its’ not easy to do but at least allows you to load it.  
If it helps you, underneath the covers we use: [GitHub - rogersce/cnpy: library to read/write .npy and .npz files in C/C++](https://github.com/rogersce/cnpy) for loading numpy arrays. The problem with loading a dictionary in nd4j is an INDArray is just one array, not a combination of them or anything.

Each array in the zip file would just be an array with a specific name, then infer the name of the array from the file.

Each file would be: name.npy.  
Then when you load it:  
You can just use:

```java
FIle f = new File("/path/to/your/file.npy");
String name = f.getName();

```

From here, just strip out the format for the name following an example like this:  
[https://www.technicalkeeda.com/java-tutorials/get-filename-without-extension-using-java](https://www.technicalkeeda.com/java-tutorials/get-filename-without-extension-using-java)

That would get you a Map\<String,INDArray\>

EIther way, make sure pickle=False when saving and save one array at a time.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 5:09am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/8 "2020-09-29T05:09:05Z")

</div>

@agibsonccc you’re a god. I have been stuck on this for an entire day with no progress and suddenly I’m progressing!

> If you need to use dicts, then I would advise separating that out. In nd4j this would be an: Map\<String,INDArray\>

I’m a bit confused as to what you mean. Do I use zip to combine all my NPY arrays?

Also, I use multi-key dicts, so I’m a bit stumped when I come to Java/Kotlin on how to create a multi-key Map object. Should I use a custom data class as a key in the Map?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 29, 2020, 5:11am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/9 "2020-09-29T05:11:40Z")

</div>

@vidheyoza the pickle appears to allow numpy to save a set of named ndarrays as a dict.  
We can’t do that. We can only load the individual numpy arrays.

Yes, you can use a zip to combine the numpy arrays if you still want to save 1 file, then you would just separate them out.

For the multi key object map, you can use a java library to do that. I would personally stay away from it and keep the names you unique. If you really want a map for that, then look at: [collections - MultiValueMap in java - Stack Overflow](https://stackoverflow.com/questions/30704568/multivaluemap-in-java)

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 5:33am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/10 "2020-09-29T05:33:05Z")

</div>

So I have a dict created from a single array so in a way my file is a dict of subarrays, but I guess numpy sees it as a dict of separate arrays.

I’m thinking of converting my dict into a single np array so that it is readable by ND4J, but in a way that I don’t lose my functionality of accessing elements from the keys as directly as possible. Will keep this post updated on my results.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 6:12am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/11 "2020-09-29T06:12:32Z")

</div>

Update: I got deeper into the problem. Looks like the problem is less due to the fact of it being a dict and more because it’s not a pure np array. I’m using strings inside the array so technically it’s an object array. Searching for a way around this.

Update 2: So any object other than int and float (and I think complex) is considered as an object by Python, especially a string that is encoded in `unicode_`. @agibsonccc or anyone else here know on how to work around this? I’m also planning to post this question on the numpy community.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 29, 2020, 9:24am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/12 "2020-09-29T09:24:18Z")

</div>

@agibsonccc

Update 3: I’ve tried many different ways to make the file that I needed, I even shifted to other formats like csv and txt but I’m just getting completely stuck at the error that INDArray does not support strings, at least not natively. Is there a way to do that in ND4J or any other library for that matter?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 29, 2020, 10:09am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/13 "2020-09-29T10:09:10Z")

</div>

@vidheyoza what is your data exactly? It’s kind of hard to tell what you want without data and a code snippet to reproduce what you want.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 30, 2020, 6:04am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/14 "2020-09-30T06:04:21Z")

</div>

What is the best way I can show a data snippet here? I’m a bit reluctant to show the data itself because it is part of IP of an organization and I’m not 100% sure I should put it out on a forum. But I can definitely describe the data here.

In crude terms, one of my data files is a 100x5 spreadsheet which I convert into a dict with 3-column key and 2-column value. Most of the columns in this file (and in others too) have string-based values in them, so I am facing difficulty in importing them into a binary npy file from python.

With this in place, I wanted a way to either load a dict-based data file in Kotlin, or have a spreadsheet based file which I can convert to a Map-like object in Kotlin.

P.S. Sorry for such a vague answer, but I wanted to be a bit careful about this.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 30, 2020, 6:21am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/15 "2020-09-30T06:21:01Z")

</div>

You could DM it to me or just make data of the same shape. We’re not looking for your data here, just to help you solve your problem.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 30, 2020, 6:32am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/16 "2020-09-30T06:32:15Z")

</div>

Got it. So as I said there are 4 files, and the structure is similar for all of them in that we have a “composite key – composite value” dict made out of it. For e.g., one of the files is of this style:

| key1 | key2 | key3 | value1 | value2 |
| --- | --- | --- | --- | --- |
| key1\_sample1 | key2\_sample1 | 0 | value1\_sample1 | value2\_sample1.mp3 |
| key1\_sample2 | key2\_sample2 | 2 | value1\_sample2 | value2\_sample2.mp3 |
| key1\_sample3 | key2\_sample3 | 1 | value1\_sample3 | value2\_sample3.mp3 |
| key1\_sample4 | key2\_sample4 | 2 | value1\_sample1 | value2\_sample1.mp3 |
| key1\_sample5 | key2\_sample5 | 3 | value1\_sample2 | value2\_sample2.mp3 |

Important things to note:

1. This is the original spreadsheet format we have, that we convert to a key-value dict in Python and store in .npy.
2. key1 and key2 are strings, key3 is an number (int to be specific), value1 is a string, and value2 is the audio file path of the same string spoken by a person or a machine (also a string).
3. The values may be duplicated between rows, but the keys will always be unique (as they should be)
4. As far as the Python code is concerned, it is just converting a spreadsheet into a dict before saving it into a .npy file. And for the Kotlin code, we want to get value1 and its mp3 audio file name as outputs when we give it a particular composite key as described above.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 30, 2020, 6:49am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/17 "2020-09-30T06:49:34Z")

</div>

> [@vidheyoza](#):
>
> - ey2 are strings, key3 is an number (int to be specific), value1 is a string, and value2 is the audio file path of the same string spoken by a person or a machine (also a string).
> - The values

So I feel like what you should try to do is setup a directory structure that reflects the key/values and indices. Make the directory structure reflect the dictionary structure and put individual numpy arrays that are pure floats underneath that. The main thing is to extract the numpy arrays and save them as npy and make sure it’s numerical data only. From there, you can just write normal kotlin that understands that directory structure.

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 30, 2020, 8:41am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/18 "2020-09-30T08:41:00Z")

</div>

Making a separate npy file for each row of the spreadsheet sounds like too much of an overkill, no? I could just make a csv and have it read by Kotlin (or any 3rd party lib if needed). It will be longer than the single npy file idea, but still faster than having 100 npy files for 100 rows.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [September 30, 2020, 9:43am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/19 "2020-09-30T09:43:08Z")

</div>

You could, but the main problem is we can only load npy files. You could do a zip file structure if you want and just load entries from that. However you want to sort the arrays is up to you, but overall the main thing is to preserve the numerical numpy arrays

---

<div class="post-metadata">

**Author:** ![vidheyoza](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vidheyoza](https://community.konduit.ai/u/vidheyoza)\
**Post date:** [September 30, 2020, 10:12am UTC](https://community.konduit.ai/t/nd4j-error-class-path-resource-cannot-be-resolved-to-url-because-it-does-not-exist/890/20 "2020-09-30T10:12:46Z")

</div>

Got it. I found a solution in another 3rd party library, I only have to figure out how to create a map now. Thanks a lot for your help!
