Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to trigger an error/warning immediately upon compile/run if an item on my class-path is missing

+5
−0

I have a very simple source file -- HelloWorld.java

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("hello world");
    }
}

I have the following compile command.

javac HelloWorld.java

If I run it, this works fine.

I have the following run command.

java HelloWorld

If I run it, this works fine too.


Now, I also have a single jar file (some.jar) as a dependency. I am not using it yet, but I will be, and therefore, want to start putting it onto my class-path.

So, I tried to run the above 2 commands with a --class-path included this time.

javac --class-path=".;some.jar" HelloWorld.java

java --class-path=".;some.jar" HelloWorld

This also worked.

Finally, I accidentally deleted my some.jar, and reran the above 2 commands.

Surprisingly enough, there were no errors or warnings from Java.

My question is -- how do I get Java to tell me that it cannot find the item I have listed on my classpath? I don't really care if the warning is upon compile or upon run, but I would like to know I made a mistake some point before my program actually tries to use the dependency (and fail because Java couldn't find it this entire time).

Basically, I want some form of confirmation or reaffirmation that Java knows where my specified items on the class-path are, and can reach them.

Please recall, this is a minimal, reproducible example. My actual problem involves WAY more moving pieces. I'm using this (admittedly contrived) example as a way to highlight my point.

EDIT - To better explain why this is a problem, let's leave the minimal example and talk about my actual problem. I am in the middle of overhauling my codebase. I am trying to update dependencies, refactor code, reorganize my folder structure, and more. In the midst of this, I run into a ClassNotFoundException. The first time, it was because I had an out-of-date version of my dependency. But the second time, it was because I had put the jar file in the wrong location, and more specifically, the location that I had put on the class-path was pointing at a non-existent jar file location.

I want to nip this problem in the bud by having Java tell me upfront if the class-path location I am providing it is pointing to nothing at all. Not being able to do that hurts the maintainability of my codebase significantly during large refactors and overhauls.

EDIT 2 - If there is no other option besides using a build tool (Maven/Gradle/etc), then I am willing to consider that. But I want to emphasize, that is a worst-case scenario solution. My project is suffering from enough complexity as is, and the last thing I want to do is uproot by build solution from the ground up.

My goal is to make small, incremental changes all over my code base. All together, it adds up to a complete overhaul, but it's made up of a bunch of tiny, bite-sized steps. Layering on an entire build system to my codebase is the exact opposite of small and bite-sized. My original build solution works great, it just ran into this problem while refactoring.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

0 answers

Sign up to answer this question »