How to compile Java code online? - java

Making a site, comparing the performance of lambda expressions in Java 8 vs. Non-lambda expressions, using html and javascript. Got most of the site done, right now the example code gets displayed by using iframes that display the .java files, then I embedded java applets that run the .class files. What I'm not sure how to do is, how can I display the code in pre tags for example and then compile the code online? That way the user can modify source code to test different stuff such as stream() vs parallelStream().

What sort of code are you trying to put into a webpage? Usually when people try to embed java applets they use the applet tag for html.
<applet code="YourMainClass.class" archive="YourApplet.jar" width="800" height="600">
</applet>
Please correct me if I'm getting the wrong idea of what you are trying to achieve, but are you just trying to embed a java applet into a website? Or are you literally trying to compile code online?

Related

Copying the code from a webpage's iframe to the Java compiler at the time of program run

Is this even possible? I want to get the JAVA code exported from the website's URL (this code will be present in an iFrame of the website) and use it as a sort-of library file.
I'm not really sure what exactly you're asking/trying to do, but it sounds like you're trying to get "some" data from a webpage. Check out jSoup, a library used to load and parse HTML webpages.

ClassNotFoundException running simple applets

I wrote http://pastebin.com/EwShF3YS for school; It's a simple java applet with a GUI pair of eyes that watches the cursor as it moves. It runs well in a compiler (NetBeans 7.4) , but for the life of me I'm having difficulty understanding why it doesn't run in a browser. I'm getting ClassNotFound exceptions. All the reading I've been doing suggests that a .class file isn't required because it's an applet, the browser should generate one at runtime. And if a class is required, how come NetBeans can run it without one?
The HTML file is stored in the same directory as the .java - and it's real simple:
<html>
<head>
<title>WatchMe</title>
</head>
<body>
<applet code="WatchMe.class" width="300" height="200">
</applet>
</body>
</html>
I've tried a variety of different things, different browsers, setting classpath; opening it on different computers. The result is always the same, ClassNotFound exception. The internet research I've been doing yields mostly unproductive answers, such as one person who completely reloaded his PC. I've noticed that if I fully qualify the path to WatchMe.class I get a hang/blank browser page. I'm completely out of ideas, so any suggestions or advice is welcome.
You need to have the compiled WatchMe.class in the same directory as HTML File. Having the .java file is not enough and no the browser will not compile anything for you. It will just run the .class file using the JRE.
All the reading I've been doing suggests that a .class file isn't required because it's an applet, the browser should generate one at runtime.
This is incorrect. You must provide either a ".class" file, or JAR files containing ".class" files for the browser to fetch.
A web browser's Java plugin is not capable of compiling Java code. If you've found a resource that tells you otherwise, it is WRONG. (I'd be interested to see concrete examples of this misinformation! Can you post the URLs?)
It is possible that your confusion arises from reading material on Javascript and thinking that it applies to Java. Don't!! They are very different languages ... and material on one does not apply to the other.
OK so why is your example not working?
It is hard to say, but the most likely reasons are:
you've used the wrong name or path in the "code" attribute,
you need a "codebase" attribute to allow the browser to map the ".class" name to the correct URL for downloading it,
your code depends on other classes (that are not in the browser plugin's class library), or
it is a bytecode version issue; i.e. you have compiled your applet with a later version of Java than is supported by the browser.
Try looking in the browser's Java console for the complete error message and stacktrace for the exception.
Try looking at the server-side HTTP logs to see what files that the client is attempting to fetch ... and what the server's response is.

view encrypted html files in jeditorpane

I am working on a swing application with javafx control (WebView). In which I have to browse HTML files either in JEditorPane or WebView.
But the problem in my project is that I am trying to view HTML files which are encrypted with the help of a software htmlprotecter . It is encrypting the files by invoking the a prompt for entering a password after that the files can be opened in browser. Is there any procedure in Java in which I can pass the password to HTML file to disable javascript and file could be opened in Java programs?
two real possibilities that i see here...
1) the javascript "encryption" (are you sure you are not confusing this with some sort of encoding?) is extremely simplistic and you can emulate the "decryption" routine yourself by operating on the HTML content within java.
2) the "encryption" is very complex and your only option is to parse and run the javascript - which means utilizing some java-based javascript engine (which i have no knowledge of)
case 2 is probably more effort than it is worth.
in short, there is no simple way to pass the password to HTML file to disable javascript. you'll either have to decrypt/decode it yourself or actually run the javascript within java.

Java Embedding Into HTML

I'm sure this question has been asked a million times, but no matter how many Google searches I do I cannot get this working. I'm basically trying to get a project with multiple packages in it to be embedded in a webpage. I made a test program which just made some balls bounce around the screen and was able to get that running. I put the main class in one package and the ball class in another just to test it and it seems to be running fine. But the program that I actually need in a web page (just called FinalProject) refuses to do it.
The best thing I can get it to do is give me a blank screen, without giving an error but just white. If I try clicking where it should be nothing happens, I think because the applet is there but is just showing white so I can't see it. I did use the applet tag, which from my understanding is now depreciated but I need to turn this project in on a webpage just so the teacher can see it. We've already tested that other people's projects (which used the applet tag) work, so I was trying to stick with that for now and worry about getting it working on every browser afterwards. Though that could very well be the problem. Maybe it would work on his browser but not mine here. I've tried running my program on Google Chrome, Mozilla Firefox, and Internet Explorer with no luck.
Here is the HTML code:
<html>
<head>
</head>
<body>
<applet code = "main.FinalProject.class" width = "700px" height = "500px"></applet>
</body>
</html>
The HTML file this is written in is in [Eclipse Workspace]/FinalProject/bin/test.htm. The FinalProject.class file referenced in the HTML exists in [Eclipse Workspace]/FinalProject/bin/ main/FinalProject.class. The FinalProject.class file acts as the main class, so I'm pretty sure that's the one I need to run. It's the one with the init(), actionPerformed(), paint() methods and all that good stuff.
Currently I'm trying to run this offline on my computer, so there shouldn't be any net URL's I would think. I used Eclipse to write the Java code, dunno if that makes any difference. Unfortunately, the Java code is rather large, too much to reproduce here, if there's something specific you think is the problem I can look and post that small section.
A few of my friends managed to get theirs working, however they said they had to remove all their .png files (annoying but doable for my project). They also said had to remove all their mouse movement code. My program is kind of dependent on that, I need that for it to work at all. I know there MUST be a way to use all the MouseListener and MouseMoveListener code online, maybe it's a little different though. I dunno if that has something to do with this, but I figured I'd point it out just to be safe.
Any help here would be greatly appreciated.
Basically you're asking something like: How to deploy a java applet for today's browsers (applet, embed, object)?
Based on that, I think what you want is:
<object
classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"
style="height: 500px; width: 700px;">
<param name="code" value="FinalProject.class">
<comment>
<embed code="FinalProject.class"
type="application/x-java-applet"
height="500" width="700">
<noembed>
This browser appears to lack support for Java Applets.
</noembed>
</embed>
</comment>
</object>
Now, you have a filename of main.FinalProject.class in your code. It seems like FinalProject.class would be more likely. But yours could be right. In any case, this html file needs to be in the same folder as main.FinalProject.class or FinalProject.class and whatever classes may also be required.
Now, you may also need to make sure your browsers can actually run an applet. See: How do I enable Java in my web browser?
Update
Based on feedback from Andrew Thompson, the preferred solution is to use JavaScript from Oracle, like this:
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
code:'FinalProject.class',
width:700, height:500} ;
var parameters = {}; // does the Applet take parameters?
var version = '1.6' ; // does the Applet require a minimum version of Java
deployJava.runApplet(attributes, parameters, version);
</script>
This requires the ability to load arbitrary JavaScript, but you could also capture that deployJava.js and have that be local as well. Might be worth a look.

Java applet using Dreamweaver

I am having a problem putting a java applet on a website using Dreamweaver CS5.
This is the page where I am having the problem:
http://www.typingsquad.com/employment.html
This is a picture of what I see in dreamweaver:
http://i895.photobucket.com/albums/ac157/daltonward1/javaAppletProblem.png
Based on what I have read on the internet, I have tried the three following methods, but only one has worked.
First, I tried putting the applet on the webpage by using media-applet. The picture posted shows how I tried to implement that. However, as you can see on the website when viewing the first box, it states that there is an error.
"java.lang.ClassFormatError: Incompatible magic value 1013478509 in class file Calculator/html"
I've done some reading on this error, but I cannot find a solution.
The second thing I tried is using an IFrame using the direct link of the html file on the server. As you can see, all that appears is a blank box.
The third thing I tried using was a hyperlink, which actually did work. No errors, but its not the effect I was going for.
Does anyone see anything wrong with my use of Iframe and/or applet on dreamweaver?
Could it be that my files aren't grouped together right on the server?
I'm really into writing Java applications and applets, but my friend just got into web development and has asked me to write some simple programs for him. Any help will be greatly appreciated and sorry if my questions could have been simply found else where as I am still new to computer programming as well.
Thanks,
James Ward
java.lang.ClassFormatError: Incompatible magic value 1013478509 in class file Calculator/html
This means the JRE is asking for a class but getting an HTML page (probably stating '404, resource/page not found') that starts with <htm. More details on the answer at the OTN.
Now I look at the source and see:
<applet code="Calculator.html" codebase = "Java Applets/Calculator/bin" width="500" height="300">
</applet>
I realize it is because the page is specifying an HTML instead of a class in the code attribute. These things do not work by magic, you need to have some idea of what you're doing if you hope to deploy an applet successfully.
If http://www.typingsquad.com/Calculator.html works as you expect, the fix will be to change the applet element in the original page to..
<applet code="Calculator.class" width="500" height="300">
</applet>
I am guessing the codebase listed in the original HTML is an artifact of your development environment. If not, you will need to add that, but if that is the case, I would strongly recommend using only directories names with:
All lower case letters.
No spaces.
i saw the html from the site.. you are saying
<applet code="Calculator.html" codebase = "Java Applets/Calculator/bin" width="500" height="300">
</applet>
you should be saying
code="Calculator.class"
the issue is that the system is getting a HTML document which is not parsable as a java class file.. just change the applet tag and things will work properly.
I'd recommend you creating a jar file and using the JAR as the codebase.. its simpler and better than having a class file

Categories

Resources