Java glitch

Started by
14 comments, last by Momoko_Fan 14 years, 6 months ago
I think I found a glitch in Java! I created a test project in Eclipse, called MyProj, and set it up as follows: MyProj has one package, abc, which is located in a folder called src. All the code for MyProj is located in: MyProj/src/abc/, including the static main driver. The following code does not work!!!:

public static void main void(String[] args)
{
    File F = new File("test.txt");
}

I made sure to import java.io.File, and the program compiles perfectly. I made sure to put "test.txt" in the MyProj/ root directory, but also tried it from within src/ and abc/ -- none of them work. What happens when I go to run the program is that it never loads, and inside the Eclipse console pane it just reads: <terminated> MyProj [Java Application] ... I tried catching for thrown errors but none are being thrown. Did I find a glitch in Java??
Advertisement
Uh, what exactly are you expecting to happen? A file object just represents a file, it doesn't actually do anything with it on it's own, nor will it print any messages to System.out, so it's not surprising that you're seeing nothing when you run it from Eclipse.

Perhaps if you explained what you expect to see any why we could tell you what you're doing wrong?
If i had to guess, it's running as normal. You aren't really doing anything, except creating an object in memory.
That line of code is causing the program to terminate, not finish. If I wrap that line of code into a GUI, such as:

public class MyApp extends JFrame{    public static void main(String[] args)    {        MyApp app = new MyApp();        myApp.setVisibility(true);    }    public MyApp()    {        // Set properties for app, such as size, menu system & child controls.        // Omitted for space.        File F = new File("test.txt");    }}


When I run the program with the File F line commented-out, it loads and appears on screen perfectly. If I comment that same line back in, the GUI all of the sudden does not appear on screen and I get that <terminated> message in Eclipse's console pane.
You should read the documentation more carefully.

Having a "main(String[] args) { int foo = 0; }" function will just create an int foo object, assign 0 to it, then terminate the program - there's nothing left to do. Same with your file object: you create a file object, assign something to it and then there are no more commands to process.

By deriving from JFrame you just tell the app to load that file and the control flows back to the GUI code in JFrame, which will just loop until you hit the X button on the window, that's a huge difference.
No, the program is terminating because it doesn't have any more instructions to perform. Your main function is completed, so the program terminates. It is perfectly valid behavior. It doesn't in the GUI example because there is code being executed from JFrame under the hood to make the GUI work. I don't know Java too well, but if I had to guess, there is another thread running.

Just a hint: if you are in 'for beginners' and think you have found a glitch in a programming language that has been around in industrial use for 14 years ... you haven't.
I sincerely appreciate your responses here, guys, but you are really not reading my original post carefully enough.

This is all obviously example code I am giving you. The actual program is just over 600 lines long, which is nothing to brag about (especially in OOP terms) but nevertheless - I'm not pasting in 600 lines of code.

What I am saying is:
(1) When I comment-out that line, the GUI launches 100% successfully; I can interact with it, call dialogs from my menus, interact with all the child controls in the main container, etc. I can use it the way it is intended.
(2) When I leave that line in (just the process of un-commenting the line) not only does my otherwise-successful GUI app not launch, I get that <terminated> error.

This is an event-based GUI application, not a line-by-line sequential console app that simply "runs out of instructions."
Quote:Original post by plywood
I sincerely appreciate your responses here, guys, but you are really not reading my original post carefully enough.

This is all obviously example code I am giving you. The actual program is just over 600 lines long, which is nothing to brag about (especially in OOP terms) but nevertheless - I'm not pasting in 600 lines of code.

What I am saying is:
(1) When I comment-out that line, the GUI launches 100% successfully; I can interact with it, call dialogs from my menus, interact with all the child controls in the main container, etc. I can use it the way it is intended.
(2) When I leave that line in (just the process of un-commenting the line) not only does my otherwise-successful GUI app not launch, I get that <terminated> error.

This is an event-based GUI application, not a line-by-line sequential console app that simply "runs out of instructions."


Well then don't tell us the code you provided does not work. It obviously does. And no, it is not obvious that it is 'example' code of the issue you are running across. We can't read your mind; you must be explicit if you want help.

What you should say is exactly what you said in this post: you have isolated the bug to that line, and it doesn't seem to be throwing any exceptions (give us examples of what you have tried catching). All you know is that when the line is commented out, the GUI launches successfully.

Do you comment out any other lines that use the f handle? Where in the application are you making this call? What is the code around the call?

File F = new File("test.txt");


If java is anything like c/c++/c# then I am guessing because it can't find the file you are looking for. This is because based on that it will look for that file in the current directory, whatever that may be. You probably need to do some code to find out your app location and use that in front of "test.txt" or change the current directory to your application path.

See if that helps at all. That is my guess is that it just is not finding the file based on the path provided. But I don't use Java so I could be wrong.
Its not a bug, its a feature!!
Okay so the answer is "we don't know."

This topic is closed to new replies.

Advertisement