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
Avatar of mccarl
mccarl
Flag of Australia image

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?
Avatar of codeBuilder
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)
SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of codeBuilder
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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of codeBuilder
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
Avatar of mccarl
mccarl
Flag of Australia image

@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?
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

If it's possible to post a link to a zipped archive of your files then that could be helpful
Avatar of codeBuilder
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"));
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of codeBuilder
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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo