I have a problem concerning the .jar Export. I have to include a .txt file into my jar from where I read words into a list. I used this to realize(in eclipse Run + Debug mode it works well):
File file = new File(Worte.class.getClassLoader().getResource("resource/wortliste.txt").getFile());
Now, if I export it into a JAR file, the JAR file is executable as it should, and it contains the list.txt and .java files in resource (as it should because it's homework ;-) ), but the program behaves like there was no file. I have the same problem when i use getResourceAsStream(). Does anybody know why this won't work? I don't understand that, because i did this 2 weeks ago with another code, and it worked o_O.
What i have tried:
deleting the Project and import into a new one
like 1, but into a new workspace
My System is a Windows 7 x64 PC, Eclipse Juno and JRE7.
Options I use for export:
[] Export generated class files and resources
[x] export all output folders...
[x] export java source files...
[] export refactorings
[x] compress the contents...
[x] add directory entries
[x] overwrite existing files without warning
jar tvf ...
39 Wed Jan 30 16:19:14 CET 2013 META-INF/MANIFEST.MF
0 Wed Jan 30 00:34:16 CET 2013 resource/
100250 Wed Jan 30 00:37:24 CET 2013 resource/wortliste.txt
0 Wed Jan 30 00:34:16 CET 2013 wortspiel/
1291 Wed Jan 30 01:19:14 CET 2013 wortspiel/BuchstabenKollektion.java
2251 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestBuchstabenKollektion.java
506 Sun Jan 27 17:38:48 CET 2013 wortspiel/UI.java
1187 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestWorte.java
2932 Wed Jan 30 01:25:00 CET 2013 wortspiel/WortspielGUI.java
4384 Wed Jan 30 01:50:40 CET 2013 wortspiel/Worte.java
310 Sun Jan 27 16:25:08 CET 2013 .classpath
383 Sun Jan 27 16:22:32 CET 2013 .project
1992 Wed Jan 30 16:02:12 CET 2013 wortspiel/BuchstabenKollektion.class
2075 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestBuchstabenKollektion.class
1104 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestWorte.class
942 Wed Jan 30 16:02:12 CET 2013 wortspiel/UI.class
4701 Wed Jan 30 16:02:12 CET 2013 wortspiel/Worte.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$1.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$2.class
3475 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI.class
You can't use java.io.File to get the contents of a file inside of a jar. Try running the following snippet,
public class TestGetResource
{
public static void main(String[] args) throws Exception
{
URL url = TestGetResource.class.getResource("/resource/wortliste.txt");
System.out.println("URL: " + url);
File f = new File(url.getFile());
System.out.println("File: " + f);
System.out.println("File exists: " + f.exists());
try {
FileReader reader = new FileReader(f);
reader.read();
} catch (FileNotFoundException notFoundEx) {
System.out.println("Caught FileNotFoundException: " + notFoundEx.getMessage());
}
}
}
You should see something similar to this,
URL: jar:file:/C:/some/path/my.jar!/resource/wortliste.txt
File: file:\C:\some\path\my.jar!\resource\wortliste.txt
File exists: false
Caught FileNotFoundException: file:\C:\some\path\my.jar!\resource\wortliste.txt (The filename, directory name, or volume label syntax is incorrect)
Instead, use Class.getClassLoader().getResourceAsStream(String) to get an InputStream for reading the file. If you still need to read the contents as a File, you could stream the contents out to a temp file.
Try this:
URL url = Thread.currentThread().getContextClassLoader().getResource("resource/list.txt");
File file = new File(url.getFile());
Related
Heavily edited/updated:
I am trying to schedule a task a long time in advance. I believe it is doing something to the process because it thinks it is not going to do anything else.
I should have used HOUR_OF_DAY for 24 hour time instead of hour, but still doesn't work correctly.
New code:
public class OtherMainClass {
private static Timer timer;
private static Calendar cal;
public static void main(String[] args) throws InterruptedException {
cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
System.out.println("Changed to: " + cal.getTime());
timer = new Timer();
System.out.println("current time is: " + Calendar.getInstance().getTime());
System.out.println("Scheduled for: " + cal.getTime());
timer.schedule(new MyTimerTask(), cal.getTime());
timer.schedule(new MyTimerCheckTask(), 0, 1000*60*10);
}
public static class MyTimerTask extends TimerTask {
#Override
public void run() {
System.out.println("in run at: " + Calendar.getInstance().getTime());
}
}
public static class MyTimerCheckTask extends TimerTask {
#Override
public void run() {
System.out.println("cal at: " + Calendar.getInstance().getTime() + " is: " + OtherMainClass.cal.getTime());
}
}
}
Commandline output (following "is:" is the calendar object passed into the schedule.):
Changed to: Fri Feb 14 08:00:41 GMT 2020
current time is: Thu Feb 13 23:25:41 GMT 2020
Scheduled for: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:25:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:35:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:45:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:55:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Fri Feb 14 09:39:27 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
in run at: Fri Feb 14 09:39:27 GMT 2020
Note the very interesting run at the end, as well as the other task running. I clicked on commandline, which turned it into select mode, then control-c to exit that, then a few seconds later, it did both Tasks. (It should have run long before I clicked on commandline.
Java version from java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)
OS: Windows 10
I ran a test stating Friday around 11:30. I changed the windows sleep settings set to 1 hour (Start -> Settings -> System -> Power & sleep). Since Friday 8:00 had passed, I changed the day to Saturday. I also changed the repeated interval to every half hour 3*1000*60*10, since I wanted to limit the lines of output. I ran the code in 5 different Command Prompts and started them at around 11:30 Friday:
No QuickEdit Mode. The program was running and I had to Crtl-c to stop it. No printout to the screen after sleep initiated. (Admittedly I didn't wait to see if it would print the 8 o'clock message after some time, because it seemed pointless.)
QuickEdit Mode. Did not click inside the screen. Same as 1.
QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that until I pressed Ctrl^c and the printouts were as if I had just started the program and it just printed the below. However I did not just exit the program, and it resumes the 30 min interval printout, as you can see from the below:
QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". Same as 4.
This is the result of 4 and 5, that only started printing after i exited "Select Command Prompt"-mode at 9:43 on Saturday:
Changed to: Sat Feb 15 08:00:16 CET 2020
current time is: Sat Feb 15 09:49:13 CET 2020
Scheduled for: Sat Feb 15 08:00:16 CET 2020
in run at: Sat Feb 15 09:49:13 CET 2020
cal at: Sat Feb 15 09:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 11:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
...
I ran another test with the windows sleep setting set to "Never". Since Saturday 8:00 had passed, I changed the day to Sunday. I again ran the code in 5 different Command Prompts and started them at around 15:30 Saturday:
No QuickEdit Mode. Worked without issues. See below.
QuickEdit Mode. Did not click inside the screen. Same as 1.
QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that. Until I pressed Ctrl^c and the printout was identical to the test above.
QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that.
Result:
Changed to: Sun Feb 16 08:00:31 CET 2020
current time is: Sat Feb 15 15:35:31 CET 2020
Scheduled for: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 15:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 16:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
..snip..
cal at: Sun Feb 16 07:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 07:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
in run at: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 08:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
The answer by Shaun Rowan to Why is my command prompt freezing on Windows 10? you provided mentions:
whenever you click on a command window in windows 10, it immediately halts the application process when it attempts to write to the console
So it doesn't seem to be a bug in the Command Prompt either, rather it's a feature.
As per Will my Window 10 apps continue to update or download on sleep or hibernation mode? by A. User:
Sleep is a power-saving state that allows a computer to quickly resume full-power operation (typically within several seconds) when you want to start working again. Putting your computer into the sleep state is like pausing a DVD player—the computer immediately stops what it’s doing and is ready to start again when you want to resume working.
If you're very interested in Windows sleep modes TechRepublic has an article on Investigating Sleep states in Windows 10 describes the 6 stages of sleep, from when the (processor clocks are stopped) -> (the processor shuts off to save power) -> (other chips on the motherboard may shut off)
Conclusion:
If you want your java program to keep executing, ensure your Command Prompt window is not in "Select Command Prompt.."-mode and also ensure that your computer does not enter sleep mode, as either of these will halt execution of the program.
I am new to java can anyone explain to me how this code works in this example
specially the LOOP portion
import java.util.*;
import static java.lang.System.out;
class FullMoons {
static int DAY_IM = 1000 * 60 * 60 * 24;
public static void main(String [] args) {
Calendar c = Calendar.getInstance();
c.set(2004,0,7,15,40);
long day1 = c.getTimeInMillis();
for (int x = 0; x < 60; x++) {
day1 += (DAY_IM * 29.52);
c.setTimeInMillis(day1);
out.println(String.format("full moon on %tc", c));
}
}
}
output is
full moon on Fri Feb 06 04:06:35 MST 2004
full moon on Sat Mar 06 16:38:23 MST 2004
full moon on Mon Apr 05 06:07:11 MDT 2004
an answer should be displayed in first three loops why it takes entire 60 runnings
What is the answer and why it should be displayed in first 3 loops?
This code creates Calendar and then added to the first full moon date the duration of lunar month (about 29.52 days).
The cycle body runs 60 times, because condition x < 60 and x++ increase x by 1.
//increase number of milliseconds
day1 += (DAY_IM * 29.52);
//set increased number to Calendar c
c.setTimeInMillis(day1);
//create formatted string
String s = String.format("full moon on %tc", c);
//output the string
out.println(s);
read more about how String.format() works in official documentation
This code will run 60 times throughout the Loop..
I realized my mistake which is iam only going by theory rather than doing practical...so guys this is lesson, and iam posting the result so that this question dosent create any unnecessary confusion further.
full moon on Fri Feb 06 04:08:48 IST 2004
full moon on Sat Mar 06 16:37:36 IST 2004
full moon on Mon Apr 05 05:06:24 IST 2004
full moon on Tue May 04 17:35:12 IST 2004
full moon on Thu Jun 03 06:04:00 IST 2004
full moon on Fri Jul 02 18:32:48 IST 2004
full moon on Sun Aug 01 07:01:36 IST 2004
full moon on Mon Aug 30 19:30:24 IST 2004
full moon on Wed Sep 29 07:59:12 IST 2004
full moon on Thu Oct 28 20:28:00 IST 2004
full moon on Sat Nov 27 08:56:48 IST 2004
full moon on Sun Dec 26 21:25:36 IST 2004
full moon on Tue Jan 25 09:54:24 IST 2005
full moon on Wed Feb 23 22:23:12 IST 2005
full moon on Fri Mar 25 10:52:00 IST 2005
full moon on Sat Apr 23 23:20:48 IST 2005
full moon on Mon May 23 11:49:36 IST 2005
full moon on Wed Jun 22 00:18:24 IST 2005
full moon on Thu Jul 21 12:47:12 IST 2005
full moon on Sat Aug 20 01:16:00 IST 2005
full moon on Sun Sep 18 13:44:48 IST 2005
full moon on Tue Oct 18 02:13:36 IST 2005
full moon on Wed Nov 16 14:42:24 IST 2005
full moon on Fri Dec 16 03:11:12 IST 2005
full moon on Sat Jan 14 15:40:00 IST 2006
full moon on Mon Feb 13 04:08:48 IST 2006
full moon on Tue Mar 14 16:37:36 IST 2006
full moon on Thu Apr 13 05:06:24 IST 2006
full moon on Fri May 12 17:35:12 IST 2006
full moon on Sun Jun 11 06:04:00 IST 2006
full moon on Mon Jul 10 18:32:48 IST 2006
full moon on Wed Aug 09 07:01:36 IST 2006
full moon on Thu Sep 07 19:30:24 IST 2006
full moon on Sat Oct 07 07:59:12 IST 2006
full moon on Sun Nov 05 20:28:00 IST 2006
full moon on Tue Dec 05 08:56:48 IST 2006
full moon on Wed Jan 03 21:25:36 IST 2007
full moon on Fri Feb 02 09:54:24 IST 2007
full moon on Sat Mar 03 22:23:12 IST 2007
full moon on Mon Apr 02 10:52:00 IST 2007
full moon on Tue May 01 23:20:48 IST 2007
full moon on Thu May 31 11:49:36 IST 2007
full moon on Sat Jun 30 00:18:24 IST 2007
full moon on Sun Jul 29 12:47:12 IST 2007
full moon on Tue Aug 28 01:16:00 IST 2007
full moon on Wed Sep 26 13:44:48 IST 2007
full moon on Fri Oct 26 02:13:36 IST 2007
full moon on Sat Nov 24 14:42:24 IST 2007
full moon on Mon Dec 24 03:11:12 IST 2007
full moon on Tue Jan 22 15:40:00 IST 2008
full moon on Thu Feb 21 04:08:48 IST 2008
full moon on Fri Mar 21 16:37:36 IST 2008
full moon on Sun Apr 20 05:06:24 IST 2008
full moon on Mon May 19 17:35:12 IST 2008
full moon on Wed Jun 18 06:04:00 IST 2008
full moon on Thu Jul 17 18:32:48 IST 2008
full moon on Sat Aug 16 07:01:36 IST 2008
full moon on Sun Sep 14 19:30:24 IST 2008
full moon on Tue Oct 14 07:59:12 IST 2008
full moon on Wed Nov 12 20:28:00 IST 2008
Okay I don't know how to explain this but if you look a
Players:
_Brand0n_:
'Fourm Link:': 'Null'
'Registered:': 'No'
'GamePoints:': '0'
'UUID:': 94bad256-7fa0-49c6-add1-50b61a854dc3
Reports:
'#R:': '0'
'TheeLux:': Hacking Time: Fri Jul 22 12:21:35 CDT 2016'
'Adam_Hall:': Hacking Time: Fri Jul 22 12:21:35 CDT 2016'
I need to be able to take the reports from TheeLux and Adam_Hall and print an msg saying
Adam_Hall: Hacking - Time: Fri Jul 22 12:21:35 CDT 2016
But I don't know how to take however many configuration sections and make them into strings ext. If this is confusing please post a comment and I'll try and clarify.
So I'm following thenewboston's tutorials on Slick 2D. I'm loading the Image exactly as he said:
private org.newdawn.slick.Image playNow;
...
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
...
playNow = new org.newdawn.slick.Image("res/playNow.png");
This is my Eclipse project tree:
This is the output it gives me:
Tue Jul 03 09:31:53 PDT 2012 INFO:Slick Build #274
Tue Jul 03 09:31:53 PDT 2012 INFO:LWJGL Version: 2.8.5
Tue Jul 03 09:31:53 PDT 2012 INFO:OriginalDisplayMode: 1366 x 768 x 32 #60Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:TargetDisplayMode: 640 x 360 x 0 #0Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:Starting display 640x360
Tue Jul 03 09:31:53 PDT 2012 INFO:Use Java PNG Loader = true
Tue Jul 03 09:31:53 PDT 2012 INFO:Controllers not available
Exception in thread "main" java.lang.RuntimeException: Resource not found: res/playNow.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.Image.<init>(Image.java:170)
at org.newdawn.slick.Image.<init>(Image.java:158)
at org.newdawn.slick.Image.<init>(Image.java:136)
at net.sourceforge.whowantsakookie.hamblaster.Menu.init(Menu.java:21)
at net.sourceforge.whowantsakookie.hamblaster.Game.initStatesList(Game.java:20)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at net.sourceforge.whowantsakookie.hamblaster.Game.main(Game.java:31)
The tutorial is located here
I am doing this exactly as the tutorial said, and it works for him in the video. Thanks in advance :)
No your problem is that the "res" folder is in the same folder as the "src" file is in!
move your "res" folder containing all of your pictures INTO the "src/net.sourceforge.whowant/" folder/package in your program.
In your program you will load it by using
playNow = new org.newdawn.slick.Image("res/playNow.png");
Hope this helps! :)
I have built a Java application that reads data from a txt file located in the src folder. The path I have specified in the program is /src/data.txt and it works when I run it from netbeans. However when I tried to open the jar file, nothing opens. So I tried using javac from command line and this gives me the error that data.txt can't be found.
How do I make sure that the data file is included in the jar so it will work as a standalone?
Thanks.
EDIT1 : Here is a snippet of the code I use to load the file. And the path used is the aforementioned /scr/data.txt
public String [] openFile() throws IOException {
FileReader fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String []text = new String[this.numberoflines];
for(int i=0;i<this.numberoflines;++i)
{
text[i]=br.readLine();
}
br.close();
return text;
}
EDIT2 : Well here is the tvf output:
Error: Could not find or load main class jar
C:\Users\Abhishek>jar -tvf Scrades.jar
0 Sun Jan 22 18:47:08 IST 2012 META-INF/
199 Sun Jan 22 18:47:06 IST 2012 META-INF/MANIFEST.MF
2562 Sun Jan 22 18:47:08 IST 2012 CombinationGenerator.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$1.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$2.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$3.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$4.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$5.class
969 Sun Jan 22 18:47:08 IST 2012 Gameplay$6.class
18279 Sun Jan 22 18:47:08 IST 2012 Gameplay.class
2275 Sun Jan 22 18:47:08 IST 2012 PermutationGenerator.class
1252444 Sun Jan 22 18:47:08 IST 2012 eng_final1.txt
3771960 Sun Jan 22 18:47:08 IST 2012 english_huge.txt
815532 Sun Jan 22 18:47:08 IST 2012 english_long.txt
16104 Sun Jan 22 18:47:08 IST 2012 english_short.txt
1506 Sun Jan 22 18:47:08 IST 2012 readFile.class
You can manually inspect, place and remove the file in the jar archive using a rar extractor like winrar : Then access per using Class.getResourceAsStream(String);
InputStream is = getClass().getResourceAsStream("/src/data.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){
//Perform operations
}
br.close();
isr.close();
is.close();