Sublime Forum

Using classes in packages added to project from other packages in the project

#1

Hello! I have a java project consisting of two packages from two different locations in my computer. One of them consists of some classes, called “main”, and the other has two .png files, called “images”. I have tried for over a week now every way I can possibly find to use the image files in the main package, but it doesn’t work. Like, for example, when I go in a file in the main package and try “import images.image1.png”, it says that the package “images” doesn’t exist. How can I make it where I can access packages in a project from the other packages in it. It know IDEs can do this, but I really don’t want to have to use one. I really like sublime text, and I feel like there is a way to successfully do what I’m talking about. I would appreciate any help. Thanks in advance!

0 Likes

Java with Sublime
#2

Hello fellow Metroid fan!

It’s hard to say for sure without more details of what you’re trying to do (and how; in particular Java is one language I haven’t tried to use with sublime yet so I’m unfamiliar with any built in support it might already provide).

The most likely culprit is that if you spread your source files across multiple locations/jar files, you need to add all such paths to the java class path so that the java compiler and runtime can find things properly.

That’s one of those things that the Java IDE’s tend to take care of. If you’re doing it yourself, you need to pass java and javac the -classpath or -cp command line option (they both do the same thing, one is just more characters to type than the other).

There is online documentation about setting the classpath available, but the short version is that it’s a list of zip files, jar files, and file system paths that point to all of the places where classes and other files might reside. Under windows, the list is separated with semi-colons while under other operating systems, colon is used.

All that said, you can’t import a PNG file in that particular manner (unless there is new functionality in Java I’m unaware of) but one step at a time!

1 Like

#3

@OdatNurd Thanks for the response! I checked around in the terminal and experimented with the classpath commands. While I believe this would work, I was wondering if you knew how to set the java classpath in the .sublime-project file, or if this is even possible. I use sublimelinter so maybe when it builds the files it would use the classpath you specify? I don’t know haha. Like when you add folders you put ’ “path”: “path/to/your/folder” '. Is there a way to do that except with the java classpath? Thanks!

0 Likes

#4

Unfortunately I’ve never used sublime linter either. :frowning: I would imagine that anything that’s designed to work with Java would somehow allow for setting the classpath somewhere since that’s integral to the whole operation.

According to this documentation fragment, it has precisely that, and even has an example of how you would set the class path.

In your case that would be a (colon on Mac/Linux, semi-colon on Windows) separated list of the paths that contain your files.

3 Likes

#5

@OdatNurd Wow that link looks like exactly what I’m looking for! Thank you very much!

1 Like