DEV Community

Khadija Umar Danhassan
Khadija Umar Danhassan

Posted on

Packages In Java

I was recently compiling a .java file in VS Code and noticed something odd: the file wouldn't run. No matter how many times I clicked the "Run" button, it just wouldn't behave — unless I dove into the terminal and ran with this command:

java FileName.java
Enter fullscreen mode Exit fullscreen mode

Here is my file structure:

Screenshot from VScode of File Structure

To run java files, VScode needs to set up a json configuration launch.json in the particular folder you run your java files from. If it is not present in that folder, then VScode has some difficulty running it when you click on "Run", unless you include this:

package FolderName;
Enter fullscreen mode Exit fullscreen mode

So, to make things run with the "Run" button in VS Code, I added this at the top of my Java file:

package Udemy_Java_for_Noobs;
Enter fullscreen mode Exit fullscreen mode

This tells Java that the file belongs in a folder named exactly Udemy_Java_for_Noobs and it runs it easy peasy. But here's where the issue kicks in

The Issue

If someone else clones my repo — or if you move the file into a different folder — that package declaration can break everything. Suddenly, the compiler complains that it can't find the class, or the folder structure doesn't match.

I realized I didn’t want people to go through that hassle, especially if they're beginners or using any environment that doesn't automatically set up a launch.json.

The Solution

To solve this, I removed the package line completely.

// No package, no problem
public class FileName {
    public static void main(String[] args) {
        System.out.println("Clean and simple.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Now the file runs from anywhere. Just compile and run like this:

javac FileName.java
java FileName
Enter fullscreen mode Exit fullscreen mode

No special folder names. No config headaches. Just Java doing Java things.


Final Thoughts

If you're building something big or working on a team? Use packages — they're useful for organizing code.

But if you're writing small Java projects, examples, or just trying to share code that runs anywhere — skip the package and keep it portable.

Hope this helps someone avoid the same confusion I ran into. Happy coding! 🧑‍💻

Here is the repo for anyone interested.

JAVA FOR NOOBS

This repository contains practice examples from the Java For Noobs course. It aims to demonstrate basic Java syntax and concepts through hands-on examples. Topics covered include:

  • Implementing conditional statements: if, else if, else, and switch
  • Utilizing loops: while and for
  • Grasping Object-Oriented Programming (OOP) principles
  • Creating classes, objects, and methods
  • Developing problem-solving skills using Java

📘 Click here to view the course on Udemy


🧪 Practice Problems

This repository contains practice problems that align with the concepts covered in the course. Each problem is designed to reinforce your understanding and provide hands-on experience.

🗂️ Solution List

  1. Problem Set 1 Problem1.java
  2. Problem Set 2 Problem2.java
  3. Problem Set 3 Problem3.java
  4. Problem Set 4 Problem4.java
  5. Problem Set 5 Problem5.java
  6. Problem Set 6 Problem6.java

🚀 Usage

Make sure JDK is installed, then run:

javac Problem1.java
java Problem1
Enter fullscreen mode Exit fullscreen mode

Or:

java Problem1.java
Enter fullscreen mode Exit fullscreen mode

🔗 Additional Resources





Top comments (0)