Display Java Source Code in HTML - java

I have some Java code that I want to display on my web site. The point of sharing this code is not for for the viewers to use, it is just for them to see. (Although I am not opposed to them using it.) It is about 10 packages, each with 10 classes. What is the best way to make it viewable and navigatable in HTML? I am fairly new at this.
I am not necessarily trying to make it pretty...I just want it to be functional. I am also trying to avoid copy and pasting 100 individual files. I want to paste the entire Java project into a folder, then allow the user to access that folder and view its contents from on the website. So something like a folder system, where when they navigate to a .java, it displays the contents of that .java below the folder tree.
How can I accomplish this? I am not really sure where to begin.

Related

Read a file from same folder as JAR file but still read resources folder when loading from IDE

I've been trying to make jar application that can read a csv file in the same directory as it. This is, however, proving difficult as my means for accessing the file currently is:
InputStream is = getClass().getClassLoader().getResourceAsStream(filename);
Which works for my program running in the IDE and for my tests but doesn't work when I run the program from the compiled jar file. I have no idea how to get it to work for both. I seriously can't understand this path stuff, it seems like there are a million ways to do it and only one of them work for only one specific scenario.
I've been trying to make jar application that can read a csv file in the same directory as it.
Ah, there's your problem. That just isn't a thing.
There are only 2 types of files:
Application Resources
These are read only, and are as much part of your app as your class files are. It is not in any way relevant to think about 'editing' them - that's not the kind of thing they are. It is reasonable to assume that if this resource is somehow missing, the app is as corrupt / misinstalled as it would be if class files are missing.
For this, you use .getResource and .getResourceAsStream. And note that getClass().getClassLoader() is wrong, you want MyClass.class.getResource and then add a slash if you want to go from root (because getClass() potentially breaks when you subclass, and going via classloader is [A] just typing for no reason, and [B] breaks in bootload scenarios. MyOwnClassName.class.getResource never breaks, so, always use that).
This asks java to look in the same place class files are and nowhere else. Your class files are inside the jar files, and not next to them, therefore, it won't find a text file that is sitting next to jar files.
it does not make sense that it does work during development: That means you shoved a file inside the resources folder, which is equivalent to having a CSV file inside the jar file. You must have gone out of your way to tell your build system to do weird things. Don't do that.
If that CSV file is not intended to be user editable it should be inside the jar file and not next to it: That makes it an application resource. Examples of application resources:
You have a GUI, and you need to store the icon files and splash screen art and such someplace.
You ship static data with your app, such as a table of all US states along with the zipcodes they use (could be a text or csv file for example).
Templates of config files. Not config files themselves.
DLLs and the like that you need to unpack (because windows/linux/mac isn't going to look inside jars for them).
You're a webapp and you want to ship the HTML static files along with your webapp.
If this is what your CSV file is, the fix is to put it in the jar, not next to it, then load it with MyClass.class.getResource(name).
Config files and project files
For example:
For a rich text editor (like, say, LibreOffice Writer), the .odt files representing your writings.
Save games for a game.
A config file, which can be edited by the user, or is edited by your own app in a 'preferences' dialog. This stores for example whether to open the app full screen or not, or authentication info for a third party API you're using.
These should not be in the jar, should not be loaded with .getResource at all, and should not be in src/main/resources in the first place.
They also should not be next to your jar! That's an outdated and insecure model (the idea that editable files sit in the same place the app itself sits): A proper OS configuration means that an app cannot write to itself which is most easily accomplished by having it be incapable of writing to its directory. Some OSes (notably, windows) did this wrong for a while.
For example on windows, your app lives in C:\Program Files\MakorisAwesomeApp\makori.jar, and the data files for it live somewhere in C:\Users\UserThatInstalledIt\Documents\MakorisAwesomeApp.
oh linux, your app might be /usr/bin/makori and the data lives somewhere in the home dir. Config data might live in /etc/.
You don't "ship" your config files, you instead make installers that create them. You can do this part in-app by detecting that the relevant config file does not exist, load in a template (that is a resource, shipped inside your jar, loaded with getResource), and write it out, and tell the user to go look at it and edit it.
I really want a CSV file next to my jars!
Well, that's wrong, so, there are no libraries that make this easy. When you want to do silly things its good that APIs don't make that easy, right?
There are really hacky ways to do this. You can use .getResource to get a URL and then 'parse' this. This breaks the classloader abstraction concept (because in java, you can write your own classloaders and they can load from anywhere, not just files or entries in jars), but you can ask for 'yourself' (MyClass.class.getResource("MyClass.class")), pull the URL apart and figure out what's happening - does it start with file://? Then it is a file, so turn it into a j.i.File object, and go from there. Does it start with jar://? find the !, substring out the jar part, and now you know the jar. Make that a java.io.File, ask for the parent dir, and look there for the CSV.
You have to write all this. It's complicated code that is hard to test. You should not do this.

Publish updatable Java software through Wordpress

I have just written a Java program that I now need to publish. I want to make the software easily updatable, and I therefore need your advice. The program will be used by people with minimal technical knowledge, hence usability is absolute key. The program is not open source.
The situation is this:
First time users download a zip-file containing one .jar-file and two folders that should contain the program output files (.xml-files and .png-files). I want to make it as easy as possible for the user to update the .jar (replace it with another .jar, not necessarily at run time). To my help I have a simple web-based Wordpress site that the user will view as the source of the program.
I'll list the possible solutions I've come up with:
1 (fallback solution). The user have to manually go to the Wordpress site where I'll put a separate direct download link (from Google Drive) for the .jar-file. The user then has to manually replace the .jar in the existing folder with the new one.The drawback to this is that it requires too much work from the user, and they program probably won't get updated very often.
2. Alongside the Program.jar in the zip-file the first user downloads, I place another Program-Updater.jar. This new .jar's sole purpose is to, when the user opens it, download a new version of Program.jar from the web and replace the existing one.This is better than the previous option because it requires less work, even though it's not automatic. The drawback is that I need one more .jar which can confuse the user, and most importantly I have no way of updating the Program-Updater.jar.
3. Java Web Start.I've tried reading up on how this works, but I'm wondering if there is a way to get it to work. One possible problem is that I can't really access the host, and to be able to set up the .jnlp the correct way I think you need this. I'm also wondering how the program's file structure would be (is the .jar even placed on the user's computer?) and if this could confuse the user. Also how to make it always work offline.
4. Suggest your own solution!
Any input on this matter would be greatly appreciated, and I'll gladly give more info than I already have.
Cheers
Getdown was the way to go; it is extremely easy to use.
I'll put the .zip on Google Drive with a direct download link to it on my Wordpress site.
The files used by getdown are placed in and downloaded from my public git repo.

How to Hide / Protect Media Folder in Java Project?

I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.

How and where to place uploaded images on glassfish 4

I'll try to be short with the description of my situation:
I'm making a restaurant recommendation web site. I want users to be able to add a new restaurant and upload 1 picture of the restaurant (restaurant's profile picture). That picture will later be displayed when users search for the restaurants. Each time a new restaurant is added I want to create new folder for that restaurant and place the uploaded picture there.
I can upload an image and place on my file system, but when I try to display it it doesn't show.
<img src="\C:\glassfish4\glassfish\domains\domain1\applications\__internal\WebAppName\profilePicture.jpg" />
This is where I was originally placing the images, but they wouldn't show.
If I understood correctly from here that is not a good practice to reference images in this manner because then the browser will be looking for that location in user's machine.
I tried to place the images in:
C:\glassfish4\glassfish\domains\domain1\eclipseApps\WebAppName
because I figured that this is where WebContent folder is (please, correct me if I am wrong), and that this location is accessible with:
<img src="http://localhost:8080/WebAppName/..."/>
but that worked only for a short time. As soon as I redeployed my app the folders in which the pictures were placed had gone away (and they were created).
So my question(s) are:
How and where to place these images, and what should my src attribute look like in an html document (should it be like C:\... or http://localhost/...)?
What are conventions, practices for this, and how is this generally done?
And does redeployment has anything to do with my pictures being gone?
I found this post, but it did not solve my problem.
Note: - I am using glassfish4, and Java Servlets, JSP, JSTL/EL, and generally Java.
Thanks in advance!
And does redeployment has anything to do with my pictures being gone?
It does. When you redeployed your application, GAS removed the application directory at ${GAS_INST_DIR}\domains\${YOUR_DOMAIN_NAME}\application{YOUR_APP_BUNDLE_NAME}. Once you decided to store you images there, they are gone after the redeployment.
How and where to place these images
The most straightforward way is to put your files somewhere outside application server folder could be a solution but I would say just half a solution. Let's assume you store your pictures in a local folder /var/application/data. Later you decided to cluster your application. Now you are again in trouble. Each instance has its own /var/application/data directory and as a rule you do not know what node will handle a request for storing an image.
What are conventions, practices for this, and how is this generally done?
I would say it is you who decides what way to go according to the needs of your application. I will list the ways that are the most obvious. All have their own strength and weaknesses.
You can put the images in a local folder. The strong side is simplicity. Once you decide to cluster, you would have to remake this approach. If you go this way the most general approach would be to create a servlet that loads your images and in this case your src= will point to the servlet. Did not find a good example right away, but I think this example will give you an idea how to do it. The only thing I would suggest using finally block or if you use jdk 1.7 the try-with-resources for closing stream. Another thing you will need to pass file name as a parameter to the servlet.
Store images in database. It could be RDBMS or noSql. Down side is that not all RDBMSs work efficiently with binary data. Again src could point to a servlet that loads images from the DB. Here you should design your DB accordingly so you can retrieve images effectively. I would not choose this approach, but this is just a personal opinion. Cannot say how efficient the noSql databases are for storing binary data. You should do it yourself
Consider Webdav. In this case your src attribute will be a link to a resource in webdav server. You can use it in clustered environment, relatively simple implementation.
and what should my src attribute
look like in an html document (should it be like C:... or
http://localhost/...)?
Depends on the approach you choose. See item 1-3.
Hope that helps.

How to embed a Java Applet from another website (can't link their class file and jar)

In addition to what my title says, I am running into problems because their class file is linked as follows:
"var attributes =
{code:'xx/xxxx/xx/xx/xxx/xxx/xxxxx.class'
width:645,height:443,archive:'xxxxx.jar'}"
First, I naively copied the HTML code and it did show a Java Applet Object, but couldn't load it because it obviously didn't find the class. I tried many different addresses to see if I can download the class, but with no success. Does this mean the class can't be downloaded? I'm in the process of asking for their permission and see if we can get it directly from them.
I also thought of another way. Is it possible to embed their whole page as an iframe AND "crop" it so the iframe only displays the area where the Java Applet is located? If this is possible, it would be the best and easiest way.
You certainly can download the the .jar file - they have to be accessible so that browsers can load them. I'd guess you are trying to get the .class file, but it is within a .jar, so get the .jar instead.

Categories

Resources