Avatar of codeBuilder
codeBuilder
 asked on

importing a source folder and running the java application

sorry for this easy question but i cannot do it. i have a java project and i want to run it , when i run it an animation will play on the screen but i cannot do it .i'm so distracted because of cannot doing it :( i'm sure that this project has no error , it is given to us as an example by the instructor.
it is a folder and this folder includes source folder and docs folder. i create a new project , then i imported the source files to the project. then i run it but it gives always and always error .please help me .
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
mccarl

You need to give us a LOT more information...

Maybe you could even tell us WHAT the error is that you are getting?

What are you "importing" it into?

Depending on how much code there is, can you post the code here?
codeBuilder

ASKER
I directly import the contents of the source code to the folder src in eclipse.
The src folder includes default package, and this default package includes all .java files. In addition , all the other extensions such as .gif .png .txt exist inside folder src.
The project is big and to put it here may cause problems , so i cannot .

Here is what i got when i run it:
javax.imageio.IIOException: Can't read input file!
      at javax.imageio.ImageIO.read(Unknown Source)
      at Simulation.<init>(Simulation.java:37)
      at SimulationRunner.main(SimulationRunner.java:23)
java.io.FileNotFoundException: input.txt (Sistem belirtilen dosyayi bulamiyor)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(Unknown Source)
      at java.util.Scanner.<init>(Unknown Source)
      at Simulation.<init>(Simulation.java:56)
      at SimulationRunner.main(SimulationRunner.java:23)
Exception in thread "main" java.lang.NullPointerException
      at Simulation.<init>(Simulation.java:60)
      at SimulationRunner.main(SimulationRunner.java:23)
CEHJ

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
codeBuilder

ASKER
you rescue me from a big trouble !! I VERY VERY APPRECIATED YOUR HELP @mccarl.
It worked when i import it in the same level with src folder.
Then could you explain why it worked in the same level rather? In tutorials i see that , .java files and .png .txt files are all inside src folder. Why is there such a difference? Is it a setting which is done with eclipse settings or in the somewhere else in the code?
CEHJ

That's not the way you should treat resources when developing with an IDE. IDEs are for developing applications for users, not developers and the app should work just as well when it's correctly packaged per best practice as a jar. The link i posted shows you how to arrange your sources and resources such that your app will work in the real world. The alternative will quite easily land you in trouble again as soon as you move it away from your IDE
mccarl

Then could you explain why it worked in the same level
The reason is that there are two basic way to load resources. The way that your instructor has coded it in this example, is loading the resources from the "file system". What has happened here is that the code is trying to load a file specified as a relative path, ie. "input.txt" and so Java is looking for that in what the "current" directory is when the code is run. In the case of Eclipse, the current directory is set to the projects base directory.

The other way, is to load resources from the classpath. If you were to use code that tries to load a file from the classpath, then the "src" folder is the root of the classpath and this is probably what you have seen in these other tutorials that you speak of. It's a little more complicated than what I have just said, but that is the basic idea! I hope that is sufficient for what you are after at the moment.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

What has happened here is that the code is trying to load a file specified as a relative path, ie. "input.txt" and so Java is looking for that in what the "current" directory is when the code is run.
So, bear in mind that it's very difficult to predict what the current directory will be when real (as opposed to IDE) code is run and therefore to stop your app breaking. That's a principal reason not to do it that way.

Of course, when it comes to having to write to files, then things are different again, but i don't think that's the case here.
codeBuilder

ASKER
I did the same thing which the link shows almost 10 times  but it didn't work,  
i click src then add package named "resources" then added all necessary .jpg txt inside it
then
i click src then add package "olgari" then added all necessary .java files inside it.
Then i changed the all paths from "ex.java" to  /resources/ex.java
But still cross signs..
It's so problematic. Why is it still Cross signs ? Is there any other thing which i should do which is not told in that link ?
problem.jpg
mccarl

@CEHJ,

I'm not disagreeing with anything that you have said at all, and if codeBuilder was writing this code themselves, I would also be encouraging them to follow your advice. But I am just wondering if you missed codeBuilder saying that this is example code given by their instructor that is just trying to be executed?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mccarl

Why is it still Cross signs ? Is there any other thing which i should do
As I alluded to in my post, you would have to go through your instructors code and change all the points where it loads resources, from accessing "file" resources to accessing "class" resources. To advise any further, I would really need to see exactly how the existing code is currently loading those resources.
CEHJ

It's so problematic. Why is it still Cross signs ? Is there any other thing which i should do which is not told in that link ?
Well, it's only the resource loading which you should be looking at there. I'm guessing that you could be changing some of the correct source (.java) stuff too. Difficult to know without seeing your full code or directory contents
CEHJ

If it's possible to post a link to a zipped archive of your files then that could be helpful
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
codeBuilder

ASKER
i loaded them like below:

background = ImageIO.read(new File("/resources/example1.jpg"));

sc2 = new Scanner(new File("/resources/input.txt"));

image = ImageIO.read(new File("/resources/example2.gif"));

Could you mean that i should call it like:

background = ImageIO.read(new Class("/resources/example1.jpg"));

sc2 = new Scanner(new Class("/resources/input.txt"));

image = ImageIO.read(new Class("/resources/example2.gif"));
CEHJ

As you'll see from my link, the code you should use is

image = ImageIO.read(getClass().getResourceAsStream("/resources/example2.gif")); 

Open in new window

but that wasn't the reason for your 'red cross' error
codeBuilder

ASKER
I included package packageName to all first line of files , red cross problem is over. But still it cannot find the input.txt file
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

:)