I changed the name of a folder so that it would meet the requirements for my assignment submission and now my program wont run in the command window. its saying it can't find the .txt files.
However, the program runs without any problems in JCreator.

i have a folder called Ass_5 with 3 folders in it - src, files and classes as well as build.bat and run.bat. src has the two .txt files, a class file and the java source file. the other two folders are empty. everything is in the right place isnt it?

Dani AI

Generated

A few practical notes based on what happened in this thread (and to avoid the same last-minute panic again):

The real issue is a mismatch between where the program looks for files at runtime and where those files actually live. IDEs and command-line launches often use different working directories and classpaths, so something that runs fine in JCreator can fail in a plain cmd shell. Instead of manually copying files every time, pick one of these robust approaches.

If the files are part of the application (read-only data shipped with the app), put them on the classpath and load them as resources. That keeps the files inside the build output or JAR and avoids dependence on the process working directory. Example:

InputStream in = Main.class.getResourceAsStream("/data.txt");

See the Java API for resource loading: Class (Java 8 docs).

If the files are external (user-editable data), make the launcher set a predictable working directory so relative paths work reliably. A simple Windows run.bat that starts in the batch-file folder:

cd /d "%~dp0"
java -cp classes YourMainClass

%~dp0 expands to the folder containing the batch file, so the process starts where the data files can be found.

Quick troubleshooting tips: print System.getProperty("user.dir") to see the current working directory, and check that the files are actually present relative to that path. For long-term builds, configure your build tool/IDE to copy non-Java resources into the classes/output folder automatically (or package them in the JAR) so the command-line and IDE behave the same.

Thanks to and for the pointers about run-time paths and hardcodings, and to for confirming the immediate fix — the strategies above make that fix repeatable and safer.

Recommended Answers

All 5 Replies

How does the src directory contain a .class file ? That tells us that everything is not in the right place.

On other thoughts, detail your problem if you want my solved threads count to go up, becuase I have a feeling I can solve this problem. It has all the characteristics of one, it is not concerning to Java, it only has limited players (a few files and directories) and on top of it , it looks trivial.

PS : Damn I should nail this one before anyone else does.

no idea how the class file got there, i certainly didnt put it there. all i did was change the directory to Ass_5.

for the program to find the .txt files they should be in the src directory shouldnt they?

i'm not sure what else to say tbh
i dont get how it will work in JCreator, but not from the command window, surely they are the same thing?

> However, the program runs without any problems in JCreator.

Check for possible hardcodings in your Java source files and the .bat files. Do a text search for the old directory name in your project directory and you might just stumble upon something.

no idea how the class file got there, i certainly didnt put it there. all i did was change the directory to Ass_5.

OSes are not habitual of moving files while the CPU is idle. ;)

for the program to find the .txt files they should be in the src directory shouldnt they?

No, they should be in the directory from where your classes are run. Typically in the classes directory.

Check from where your JCreator is running your classes.

wahey i've done it!!! JCreator was running classes from the src directory. just copied the text files across to the classes directory and its working now. thanks for you help, i was starting to panic a bit, i have to hand it in in an hour!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.