I'm brand new to java and have tried to get this to work for the last 48 hours and I'm about to give up.
I want to put the java applet on a website. Works fine only in eclipse. I tried many solutions already suggested on this site and none of them worked for me and just mucked up the code so I've reverted it to my original state. Could anyone pinpoint the problem? Thanks!
(code edited to reflect suggested answers)
package nameapp;
import java.util.*;
import java.io.*;
import java.applet.Applet
public class NameApp extends Applet{
public static void main(String[] args) throws IOException {
String name;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name? ");
name = reader.readLine();
if (name.equals("Mike")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I am totally awesome!!!");
}
else if (name.equals("Lisa")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm the prettiest gal in the world!");
}
else {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm just ok i guess...");
}
}
}
And html is...
<applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
please read this tutorial that's contains basic stuff about JApplet,
please use JApplet not Applet
carrefully read signed java applet restrictions
What Applets Can and Cannot Do
https://stackoverflow.com/tags/java-web-start/info
note with Java 1.6.025 comings another restrictions for JApplet, and these problems and possible workaround are detailed described on this forum by Andrew Thompson, but I lost link ...
It looks like you are writing an application rather than an applet. When you run it in eclipse, do you select Run As... and then select Java Application? Try running it as a Java Applet instead. You should see appletviewer pop up with no content in it.
The entry point for an applet is the init() method, not main(), and the graphics related method paint() is also usually overloaded; I haven't seen an applet that has access to standard in and out.
You might find the stackoverflow question here useful: Main vs init.
code="nameapp/NameApp.class"
Put it would also need to extend java.applet.Applet and generally be an applet.
Read the Applet tutorial - Instead of a public static void main(String[] args) method, an Applet needs public void init().
Also, with the code you have now, you'll just see a blank Applet - you'll have to have the Java Debug Console up to see anything printed by System.out.println(), and Applets can't access System.in to read input - instead you'll want to add some TextField components to your Applet and read and write the text with those.
Related
import java.util.Scanner;
public class Main {
public static void main(String [] args) {
Scanner sc=new Scanner (System.in);
System.out.println("Enter any two numbers: ");
int a,b,c;
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println("The sum = "+c);
}
}
I did the same program on VS Code - it ran without issue.
I used the following online IDEs:-
tutorialspoint.com (same error)
jdoodle.com (executed successfully)
onlinegdb.com (executed successfully)
programquiz.com (executed successfully)
online-java.com (executed successfully)
w3schools.com (same error)
interviewbit.com (same error)
I had to use an online IDE for my interview - I don't remember.
I have tried to understand the exception, but I cannot align my issue with what the exception is all about. I've read multiple stack overflow threads about this issue. Some people are saying, I should not use close() - but I haven't even used it! Others are giving solutions that are not related to my issue at all.
Please give me some directions or hint so that I can learn from this problem.
I think it is more on how the IDE is written, may be it is not giving you an interactive screen to give an input and expects you to do that separately.
Just checked this for w3schools.com and interviewbit.com where we have a separate section for giving the input and this code runs perfectly fine there.
See both the screen shots attached
sorry if this question was repeated earlier, but I couldnt seem to find the answer. I have a code:
import java.util.Scanner;
public class PracticeProblems {
public static void main(String[] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
x= input.nextInt();
if(x%2 == 0) {
System.out.println(x%2==0);
}
else {
System.out.println(x%2==0);
}
}
}
Now I am not too worried if the code actually works or not (because this same problem has occurred when the code was perfectly functional) I am just as confused why it doesnt run.
Try running by right clicking on PracticeProblems.java, and choosing "Run As Application". It looks like it is trying to run ComputeArea, and can't find that class.
I had a similar problems too.
Eclipse > Projects > Clean
if its not working try to copy the class. Delete the Project and recreate it
I have faced similar problems many times.
To solve this problem save the program, close Eclipse, reopen it and run the program without any loss of the program.
It works fine.
I'm trying to write a parser to a file called "x". I want to use scanner. I tryied to follow actions from a tutorial: https://www.youtube.com/watch?v=3RNYUKxAgmw.
package q;
import java.io.File;
import java.util.Scanner;
public class Parser {
public static void main(String [] args) {
Scanner x = new Scanner(new File("/home/x/eclipse-workspace/q/src/q/x.txt"));
String s=x.nextLine();
System.out.print(s);
}
}
The file that I want to open is called "x", its text file. We can see it in Package Explorer on left side. I clicked right on its properties. There is visible file locatization.
There appears FileNotFoundException as on the picture. I doesn't understand why this file cannot be opened.
[update] But I'm not sure if this is what
There appears FileNotFoundException as on the picture. I doesn't
understand why this file cannot be opened.
That's not what's happening. The error is in compilation time (the program has not executed, it doesn't know if the file -will- exist). The compiler is telling you "this method/constructor, according to its declaration, can throw an Exception (in this case: a FileNotFoundException ) at run time; you have not told me what to do in that case".
You really need to read about how Exceptions are treated in Java.
For a quick remedy, add a throws Exception to your main declaration. (Bear in mind: that is an awful thing to do if you don't really understand what are you doing)
So we want to use the bog-standard keytool utility that ships with a JRE. But rather than going through the trouble of finding the correct path and executable extension, spawning a subprocess, and running the executable, we collectively had the bright idea ("remember, none of us is as dumb as all of us!") to just call KeyTool's main() directly. It's implemented in Java code and also shipped with the JRE, and contains the standard "classpath" exception to the GPL so we can link against it.
Looking at the KeyTool source, there's even some provision made for this sort of thing: there are comments like "if you're calling KeyTool.main() directly in your own Java program, then [helpful reminder]" and the top-level main() is capable of propagating exceptions to calling code instead of just dying with System.exit(). Being able to just build the same command-line argument array and run KeyTool.main(stuff) instead of having to mess with platform differences seems like a very Java-esque thing to do, right?
In practice, weird things happen and we don't know why.
We want to capture any output from running KeyTool, which starts off like this:
// jdk/src/share/classes/sun/security/tools/KeyTool.java, line 331:
public static void main(String[] args) throws Exception {
KeyTool kt = new KeyTool();
kt.run(args, System.out);
}
private void run(String[] args, PrintStream out) throws Exception {
// real code here, sends to 'out'
}
The KeyTool entry points don't allow us to pass a PrintStream, it's hardcoded to use System.out. That should be okay thanks to System.setOut. We have an OutputStream subclass which feeds to a JTextComponent, but for initial coding, redirecting to a text file is fine. So our code does
PrintStream orig = System.out;
try {
System.out.println("This is the last visible console line");
System.setOut(new PrintStream("redirect_test.txt"));
System.out.println("This is now redirected!");
KeyTool.main(keytool_argv); // "-help" and "-debug" for now
}
catch all the myriad ways things might go wrong { ... }
finally {
System.setOut(orig);
System.out.println("Back to normal console output");
}
But when we run the code, the redirect_test.txt file contains only "This is now redirected!". The output from keytool's "-help" still shows up on the console, along with the before-and-after println calls.
There are some other oddities in calling KeyTool directly, like the package and class name has changed between Java 7 and Java 8, but that's easy to deal with via reflection. (The comments in the KeyTool source in Java 8 still refer to the Java 7 name, heh.) The only thing just freaky weird is how its "System.out" is strangely not affected by the same redirection that works everywhere else. (No, there are no weird import statements bringing in a special System replacement.)
Here's an online copy of Java 7's KeyTool.java if you don't happen to have OpenJDK sitting around.
You just need to redirect both System.out and System.err, since the usage instructions get printed to the standard error stream instead of the standard output stream. Try this:
PrintStream original = System.out;
PrintStream redirected = new PrintStream("redirect_test.txt")
try {
System.out.println("This is the last visible console line");
System.setOut(redirected);
System.setErr(redirected);
System.out.println("This is now redirected!");
KeyTool.main(keytool_argv); // "-help" and "-debug" for now
}
catch all the myriad ways things might go wrong { ... }
finally {
System.setOut(original);
System.setErr(original);
System.out.println("Back to normal console output");
}
i am learning java at this moment and i've read somewhere that i can also run my java codes in a browser with an applet. if i try to use it, it says error click here for details.
the problem is that the java program does work in command prompt but not in the browser.
here is my HTML code. (Planet.class and the html file are both on my desktop)
<applet code="Planet.class" width=500 height=500 />
this is my java code (this code works nice in command prompt; don't try to get what it's exactly doing it just makes a rhombus out of *'s.) :
class HelloWorldApp {
public static void main(String[] args) {
int n=20;
int i;
int k;
int j;
for (i=1;i<=n;i++)
{
for (k=n-i;k>=0;k--)
{ System.out.printf(" ");}
for (j=1;j<=2*i-1;j++)
{System.out.printf("*");}
System.out.printf("\n");
}
for(i=n-1;i>=1;i--)
{
for (k=0;k<(n-i)+1;k++)
{ System.out.printf(" ");}
for(j=2*i-1;j>=1;j--)
{System.out.printf("*");}
System.out.printf("\n");
}
}
}
i believe that it is just because the printf isn't supported in the browser but maybe i'm doing something else totally wrong please tell me.
It is not an applet. For this class to be an applet it would have to inherrit either Applet or JApplet.
Applets don't use main method (they have other lifecycle methods)
You were right --- system out does not end in a webpage but in java console.
Please read http://docs.oracle.com/javase/tutorial/deployment/applet/index.html
Your class must extend JApplet. If you use NetBeans, you will get some generated code and you can start learning from there.