I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this
Related
Sorry if the title doesn't say what I'm trying to do very accurately but I wasn't really sure how to describe it in one sentence. Basically I have a jar file with a java file inside it, and I want to edit the java file. I used 7zip to do so and it worked, letting me edit the code in the java file. I saved it and my new code shows up when I open the java file in netbeans. However, the new code isn't actually in effect, and my program is still using the old code. I don't know if this matters, but the java file is named Templates.java, located in a package called net.sf.dynamicreports.examples in a jar named dynamicreports-examples-5.0.0-sources.jar.
Thanks in advance for any help anyone can offer.
Edit: tried importing the jar into a new project and editing from there before putting it back into the original project, but had no luck (check comment chain for further details). Still have no idea how to fix this and would greatly appreciate any help
Your jar contains .class files and .java files.
When you create the jar, the .java files are compiled and .class files are created from them. You should have a Templates.class file that corresponds to Templates.java.
When you edited Templates.java, Templates.class was never recompiled. This class file is the one your program uses.
The appropriate way to change a jar file is unfortunately to regenerate the entire jar.
So, what you should do is open the code you used to create the jar in the first place, make your java changes there, and create a new jar.
You will need to do this even with the smallest changes.
I didn't make the jar myself unfortunately, I got it from somewhere else so I don't have the code used to make it, just the jar itself. What should I do?
Edit: I saw your comment that you did not make the jar in the first place. This isn't too big a problem since you do have the java source code. What you need to do is copy all the .java files in the jar and make a new jar with them. Or, simply use Eclipse or whatever your favorite java editor is to compile your Templates class, and copy the new Templates.class file it creates into the appropriate place in the old jar, overwriting the old one.
Where would I find the Templates.class file?
Edit 2: You find the class file in the bin folder of your project if you are using Eclipse. See this question: Find the .class file compiled by Eclipse or if you are using a different editor/compiler, search google for where it stores its .class files.
I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.
The Netbeans created Jar does not work, but inside the IDE program it works perfectly.
I believe that the main class is set, so I'm not sure what the problem is, I think it might have something to do with the txt files I'm using, in the IDE they are in C:\Users\J\Documents\NetBeansProjects\PointOfSale\src\pointofsale (the text files are with my java files). After building the dist/ jar though the text files are inside the jar with no folders or anything (Jar is in C:\Users\J\Documents\NetBeansProjects\PointOfSale\dist). I this this might be the problem, if its helpful, I access the files using
File file = new File(System.getProperty("user.dir")+"\\src\\pointofsale\\list.txt");
You need to use Class.getResourceAsStream() to load the file. It searches from inside the classpath (and therefore from inside the jar). Now you can't load the list.txt because it doesn't exist in the directory you're specifying, it's inside your jar.
Something along the lines of
getClass().getResourceAsStream("list.txt"); // Or "/list.txt"
Will give you an InputStream you can use to load the file contents.
I have a Java JAR file that was developed to run in the browser. It works fine. There doesn't appear to be any sort of build file associated with the source code I've been given and I'm attempting to create a project and/or build script.
The source code references some third party code. What I've done is unzipped the JAR file into a folder that has this structure:
\App\src\com
\App\src\META-INF
\App\src\applet
The "com" folder contains subfolders which contain the .CLASS files for the third party libraries. "applet" contains the .JAVA source code files (one folder, maybe 15 files).
The code appears to be written in 1.4 syntax (I manually attempted to compile some of the .JAVA files and it complained that some things were deprecated and/or required the older version).
I created a Java Project in Eclipse and pointed it to the \App folder. Then I went into the Properties for the project and went to Libraries and and clicked on "Add Class Folder" and added the \App\src\com folder. Then I went to "Order and Export" and moved this to the top. I also set the Java Compiler version to 1.4.
I am getting a large number of compiler errors, all of which seem to stem from the fact that it says it can't resolve an import. This import is the set of .CLASS files contained in the "com" subfolders. The namespace matches the directory structure.
This seems like it should be really straightforward, but I've tried various things and don't seem to be making any progress. Based on what I've read I'm sure it has something to do with the pathing but I'm at a loss at this point on how to fix it.
(I'm sure it goes without saying that I'm not a Java dev.)
Any thoughts or ideas as to where I'm going wrong?
Can you please try to right click on Eclipse's Navigator. Select Import>General>archive file> (Jar file that you want to import).
You should be getting the jar file correctly imported at this point. Do tell me if you face any problems.
How do I create a jar with Ant that includes the source attachment?
Maven does this, but I'm living maven-free since '03 (well, 2009, but it doesn't rhyme).
I can't see any options in the javac task which does this.
I tried including the source .java files in the fileset passed to the javac task, but this didn't work. When I try to view the source through something like Eclipse, I still get the 'no source attachment' display.
Any suggestions?
You need two things to make it work in Eclipse:
Include the source files in the file set, so that each .java file exists in the same folder in the JAR as the corresponding .class file;
In the <javac> task, add the following parameter: debug="on". That way, you encode information into the .class files about the corresponding .java files that tools like Eclipse need in order to connect the two together.