0

I thought when Java classes are in the same directory, you don't need to import another class when you use it inside another. I have this class that will initiate Cloudinary uploading of files back to the cloud, but when I call it inside another class and run build I get this error 'cannot access CloudinaryUpload'

The Cloudinary class

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;

public class CloudinaryUpload {

    public static void main(String arg[])throws Exception{
        Map config = ObjectUtils.asMap(
            "cloud_name", "name",
            "api_key", "api_key",
            "api_secret", "api_secret",
            "secure", true
        );
        Cloudinary cloudinary = new Cloudinary(config);
    }
}

Just a snippet of code of another class where I am calling it

public class ClientManagerServices {

    private static final int BYTES_DOWNLOAD = 1024;
    
    //The Cloudinary class
    private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();
    public static String getMessageBody(Delegator delegator, String requester, String subject, String registryFileId, String clientId) {

        GenericValue fileData = null;
        GenericValue userData = null;
        GenericValue clientData = null;
        String bodyToReturn = "";

        try {
            fileData = delegator.findOne("RegistryFile", UtilMisc.toMap("registryFileId", registryFileId), false);
        } catch (GenericEntityException e) {
            e.printStackTrace();
        }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MAXWEL OCHIENG
  • 170
  • 1
  • 14
  • 2
    Cannot see the "package" declarations at the file tops. Take a careful look at your imports too that they match packaging levels properly. – Samuel Marchant Sep 23 '22 at 07:17
  • `private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();` In this, are you creating the object of `CloudinaryUpload` class, then _CloudinaryUpload()_ function is return the instance of that class or you forgot it? – Sawan Meshram Sep 23 '22 at 07:28
  • yes I am creating the object to access the cloudinary config – MAXWEL OCHIENG Sep 23 '22 at 07:37
  • https://stackoverflow.com/users/5354987/samuel-marchant They just import at the top – MAXWEL OCHIENG Sep 23 '22 at 07:43
  • You are not creating a class, you're calling a method `CloudinaryUpload()`, which does not exist. Did you mean to use `new CloudinaryUpload()`? As an aside the `main` method in `CloudinaryUpload` doesn't make sense to me, it doesn't really do anything. Do you maybe think that `CloudinaryUpload()` would somehow execute the `main` method (it does not, that is not how Java works). – Mark Rotteveel Sep 23 '22 at 07:50
  • Okay maybe I am not getting the gist of this java thing, How can I use the CloudinaryUpload inside ClientManagerServices class? – MAXWEL OCHIENG Sep 23 '22 at 07:54
  • I wanted A reusable class that I can use multiple times – MAXWEL OCHIENG Sep 23 '22 at 07:58
  • 1
    Then you shouldn't put things in main. Read any introductory Java text that covers object oriented programming. The local variables you define in main should probably be fields. Then you also need accessors or other methods that do something with those fields. – Mark Rotteveel Sep 23 '22 at 08:10

1 Answers1

3

This line:

private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();

If that is supposed to creating new instance it should be

private CloudinaryUpload cloudinaryUpload = new CloudinaryUpload();

Note ... new. If you leave out the new, then that is a call to a method named CloudinaryUpload ... and no such method exists in your code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216