Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Reading HTML  (Read 2226 times)
0 Members and 1 Guest are viewing this topic.
Offline Ricks01

Junior Newbie





« Posted 2006-03-12 14:51:34 »

Hello All,

I'm not totally new to Java.  I've been messing around with it for the past couple of years.
Not until the past couple of months have I decided to become serious about it.


Which leads me to my question.  I've mostly only done stuff with graphics and some controls here and there, but now I have come across an apllication where I need to read HTML, perform operations on it, and store it in a text box or something where I can copy it to the clipboard.  However, I am stuck on step 1 - actually reading the file.

Here is my code thus far in applet form:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.lang.Math;
import java.util.Date;
import java.util.Random;
import java.awt.event.*;
import java.applet.AudioClip;


public class myAppletIO extends java.applet.Applet {


URL myURL;
URL url;


URLConnection urlconnection;
InputStream myIS;
String yep = "wrtjwhrtkj:";

public void init() {

   String s;
   setBackground(Color.yellow);
   setForeground(Color.blue);
   yep.concat("Hello!");
   String inputLine;  
   DataInputStream inStream;



        URLConnection connection;
       
   //Step 1&2: Create URL and URLConnection instances
  try{
      url = new URL("www.yahoo.com");
   }catch(MalformedURLException e) {yep.concat("Can't access URL");
///               yep.concat("String: " + e.toString());
     System.out.println("String: " + e.toString());
//                yep.concat("Message: " + e.getMessage());
     System.out.println("Message: " + e.getMessage());
                repaint();}

/*
   try{
      myURLConnection = url.openConnection();//
   }catch(IOException e) {System.out.println("Can't open connection to URL");
                       yep.concat("Can't open connection to URL"); repaint();}
*/


   //Step 3: Get InputStream associated with URLConnection
/*   try {
      myIS = myURLConnection.getInputStream();      
   }
                    catch(IOException e) {System.out.println("Can't open connection to URL");}
*/


   //STEP A
//                   InputStreamReader myISR = new InputStreamReader(myIS);

   //STEP B
//                   BufferedReader BR = new BufferedReader(myISR);




/*                  for(int i=0; i<3; i++)
                    {

      try {               
                s = BR.readLine();
                System.out.println("Line " + i + ": " + s);
      }catch(IOException e) {System.out.println("Line " + i + ": " + s);}
                    }

                    repaint();
                   //Close all streams
                    try{
           BR.close();
           myISR.close();             
      myIS.close();
   } catch(IOException e) {yep.concat("can't close streams"); }
*/
}
    public void paint(Graphics g) {
       
          g.drawString(yep,60,100);

   
    }         



}


I guess my first question would be:  Can this be done with an applet? 

As is, the applet compiles and runs successfully.  However, when the second and / or third try blocks are uncommented, the code compiles successfully, however when it is run, the applet won't init or I get the applet area box with the red X in it.

I also tried an application thinking that this might be an applet limitation thing (this is an example from a book):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
import java.io.*;
import java.net.*;
import java.util.Date;

class URLConnections1
{
    public static void main(String args[]) throws Exception
    {
        int character;
       
        URL url = new URL("www.yahoo.com");
        URLConnection urlconnection = url.openConnection();

        System.out.println("Content type: " +
            urlconnection.getContentType());
        System.out.println("Document date: " +
            new Date(urlconnection.getDate()));
        System.out.println("Last modified: " +
            new Date(urlconnection.getLastModified()));
        System.out.println("Document expires: " +
            urlconnection.getExpiration());
       
        int contentlength = urlconnection.getContentLength();

        System.out.println("Content length: " + contentlength);

        if (contentlength > 0) {
            InputStream in = urlconnection.getInputStream();

            while ((character = in.read()) != -1) {
                System.out.print((char) character);
            }

            in.close();
        }
    }
}


However, when I run this after I compile it, I get a
Quote
"Exception in thread "main" java.lang.NoCclassDefFoundError:  URLConnections1

The java version I'm using is 1.4.0 and sometimes 1.4.1.  I've looked at many examples and consulted the book I've been studying from, Java 2 Black Book (which the app example came from), but I just can't seem to get past this. 

If anybody would have any suggestions or input, I'd really appreciate it.

Thanks,

Rick
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2006-03-12 15:50:16 »

Your applet is throwing a SecurityException when it tries to access Yahoo.com. Since you don't catch this situation, the applet dies before it finishes starting. Applets can only access the server they were downloaded from, so they are a bad place to be doing network experiments.

Your application looks fine. It's probably just some program that was installed (like Quicktime) that auto-screwed your classpath. Try running it like this:

1  
java -cp . URLConnection1


Make sure you're in the same directory as URLConnection1.class. If that works, you can fix your system by right clicking on "My Computer", selecting "Properties", going to "Advanced", clicking on "Environment Variables", and deleting the "CLASSPATH" entry from both boxes.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Ricks01

Junior Newbie





« Reply #2 - Posted 2006-03-12 19:29:19 »

Hello jbanes,

Thanks for your reply.  Yeha, I was thinking the same thing about applets.

Anyway, when I run the application using the -cp switch, I get the following (running it in the directory of URLConnections1):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
 -hotspot      is a synonym for the "client" VM  [deprecated]
               The default VM is client.

 -cp -classpath <directories and zip/jar files separated by ;>
               set search path for application classes and resour
 -D<name>=<value>
               set a system property
 -verbose[:class|gc|jni]
               enable verbose output
 -version      print product version and exit
 -showversion  print product version and continue
 -? -help      print this help message
 -X            print help on non-standard options
 -ea[:<packagename>...|:<classname>]
 -enableassertions[:<packagename>...|:<classname>]
               enable assertions
 -da[:<packagename>...|:<classname>]
 -disableassertions[:<packagename>...|:<classname>]
               disable assertions
 -esa | -enablesystemassertions
               enable system assertions
 -dsa | -disablesystemassertions
               disable system assertions

I assume it didn't like something as I couldn't see the top.  Darn it!

And, when I run it (within the directory) without the -cp switch, I get........
1  
2  
3  
4  
5  
6  
7  
8  
9  
C:\Java_2>java URLConnections1
Exception in thread "main" java.net.MalformedURLException: no protocol: www.yaho
o.com
        at java.net.URL.<init>(URL.java:579)
        at java.net.URL.<init>(URL.java:476)
        at java.net.URL.<init>(URL.java:425)
        at URLConnections1.main(URLConnections1.java:11)

C:\Java_2>

I wonder if it is some of the classpath crap.....

Going to try something and restart Windows.....

Rick
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ricks01

Junior Newbie





« Reply #3 - Posted 2006-03-12 19:43:59 »

Nope, that didn't help.

I think there is something wrong with the classpath stuff, because I made a change a while ago here, and now when windows starts up, it can't find win.com in the windows directory, and I always have to change it to the Windows directory and type win.  I'm still running Windows 98, BTW.
Here's what the paths look like.  Tell me if you can see something obvious.  I can't right away.:
Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2006-03-12 22:28:27 »

Hello jbanes,

Thanks for your reply.  Yeha, I was thinking the same thing about applets.

Anyway, when I run the application using the -cp switch, I get the following (running it in the directory of URLConnections1):

Did you actually put a classpath after the -cp ??

In general seeing the help means your java comamnd line was mal-formed.  Show the whole thing to us and we'll tell you why.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Ricks01

Junior Newbie





« Reply #5 - Posted 2006-03-13 01:01:55 »

Hi Jeff,

No.  (Boy, I feel stupid).  I just now typed and got:

1  
2  
3  
4  
C:\Java_2>java -cp c:\j2sdk1.1.1_01\lib URLConnections1.java
Exception in thread "main" java.lang.NoClassDefFoundError: URLConnections1/java

C:\Java_2>

The URLConnections1.java and .class files are located in the Java_2 directory.

I guess that's the pre- Sunday nap before work thoughtlessness.  Wink

Do you think there's not enough stuff imported??  It's actually compiling so it's finding the bin folder alright.

That's the only thing I can think of....

P.S.  I'm hoping to make this and possibly other stuff a stand alone application - Can you guys/ Do you guys recommend developing java applications and making them standalone via another application or would you recommend just using Windows, in which case that would open up another ball of wax?

-Rick
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #6 - Posted 2006-03-13 03:03:40 »

1. There was a period in the command I gave you that pointed to the current directory. You have to watch out for those. Smiley

2. In the screenshot you posted, do you see the part that says "CLASSPATH="? Delete it. It's going to cause you a lot of pain and anguish otherwise. Dealing with the classpath manually is almost always better.

3. Don't add ".java" or  ".class" to the end of the command. It's precisely what follows:

1  
java -cp . URLConnections1


Paste that into a command line window (in the correct directory!) and I guarantee it will work. (BTW, wasn't the name URLConnection1 and not URLConnections1?)

Quote
P.S.  I'm hoping to make this and possibly other stuff a stand alone application - Can you guys/ Do you guys recommend developing java applications and making them standalone via another application or would you recommend just using Windows, in which case that would open up another ball of wax?

Standalone Java applications are a great option. I always recommend them over Applets. But you can do your entire program in Java. There's no need to have a Windows front end. If you're worried about the look and feel, just set Swing to the OS Specific Look and Feel.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Ricks01

Junior Newbie





« Reply #7 - Posted 2006-03-13 04:16:02 »

1.  Ahhh, thanks, ok!  Did that.

2.  Yes, deleted that.  That might rectify some of my other Windows problems.

3.  And, yes retyped it as you have quoted.

Quote
Paste that into a command line window (in the correct directory!) and I guarantee it will work. (BTW, wasn't the name URLConnection1 and not URLConnections1?)

No, actually, it is URLConnections1.  The original example is URLConnections; however, I copied it over to my working directory and modified while I wa trying to solve this problem before- so I just copied the original back over and called it URLConnections1 just to see if it would compile.

I have really been interested in java lately and if I can develop stand alone applications that are comparable to a Windows application than that is the way I would like to start.  I actually have a copy of Windows Visual C++ 6.0, but just never pursued Windows programming seriously.

Well, I retyped it as you said and got the Malformed URL error again.....

BUT, I went and looked at the code, and I put the http:// in front of the www.yahoo.com and recompiled it, and then ran it and it worked!!

Now, do you typically create stand-alone java applications?  Is this a common practice?

Well, I have to go to work now...  Sad  I work 3rd shift.  Yuck!   Sad 

Thanks for all of your help, j!

Rick
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #8 - Posted 2006-03-13 04:54:32 »

Now, do you typically create stand-alone java applications? Is this a common practice?

Yes.

Applets are dead. Long live the applet.

* jbanes keeps beating an applet while muttering something about the fact that it just... won't... DIE

Java Game Console Project
Last Journal Entry: 12/17/04
Offline cylab

JGO Knight


Medals: 34



« Reply #9 - Posted 2006-03-13 11:04:49 »

Quote
I'm not totally new to Java.  I've been messing around with it for the past couple of years.
Not until the past couple of months have I decided to become serious about it.

You might consider using an IDE to make things easier. The most common free exponents are eclipse and netbeans. I prefer netbeans, but it's a matter of taste.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ricks01

Junior Newbie





« Reply #10 - Posted 2006-03-13 15:58:28 »

Hi Cylab,

Thanks for the input.  Yes, I would like to use an IDE.  I actually read the thread 3 weeks ago, a month or more or something about where to start.  And, I actually downloaded netbeans as this seemed to be maybe a little favored over eclipse.  Although a lot of people liked eclipse too.  I looked at it as well.

Unfortuanetly, I'm still running Windows 98 (I'll explain in a sec.), and after I downloaded netbeans and tried to run it, it would not run.  And, eclipse, looking like a good contender, is a huge download - maybe 100MB or more?  Can't remember.  And unfortuanetly, again, I have satellite service, and you know - they have the fair use policy crap - which is good.   

Why am I still running Windows 98 SE?  Because I can't justify paying a $100 or more on a new operating system, when I could upgrade my computer for probably a couple hundred more.  Why haven't I upgraded my computer?  Because I've been professionly unemployed now (because I was burned out) for over a couple years now and funds are kind of tight.

Anyway, I guess my real question is that when you create a stand-alone application, will it be a true stand-alone application not requiring any extra java files?  In other words, will it be an .exe?
Offline cylab

JGO Knight


Medals: 34



« Reply #11 - Posted 2006-03-13 16:23:51 »

Quote
In other words,Will it be an .exe?

Short answer:
No

Longer answer:
There are several ways to produce a double-clickable executable. First of all, if your user has a recent JRE (1.3+?) installed, you can create a double-clickable ".jar", if you provide a "Main-Class:" attribute in your Manifest.mf (search for it). Another possibility is to use Java Webstart, so your user can just click on a link in their browser and the application gets startet. You will have to create and link a ".jnlp"-file to support this (search google as well). A JRE is required here as well.

If you want to avoid, that your users have to install a JRE, you can include it in your distribution (as long as you respect the redistribution-licence, see the README.txt in your JRE installation directory). To be able to start your application without preinstalling the JRE you can provide a setup-script (eg by using Inno-Setup that generates a ".lnk"-file in the users start menu or on the desktop that contains the right command (eg. "C:\Program files\YourApp\jre\bin\java.exe -cp C:\Program files\YourApp\app.jar org.yourorg.YourApp").

The last option would be to use a java2native compiler like GCJ which is included in the GNU C++ compiler "GCC" 4.0, but you need a bit of knowledge using it.

Btw. whats the Problem running Netbeans?

Mathias - I Know What [you] Did Last Summer!
Offline Ricks01

Junior Newbie





« Reply #12 - Posted 2006-03-14 02:36:06 »

Hi Cylab,

Wow.  Thanks for all of the information.  I will definitely consider it. - possibly the last option.

I was mistaken about the problem with netbeans.  It actually appears to run.  However, when it gets to the point of actually installing the program to a directory, it gets an error:  "Error: C:\Netbeans is not writable"  It does this with any directory I choose or create. 

I think this is a I'm running on a crippled version of Windows problem.  I'm not going to reinstall Windows again, because I've probably done it 3 times since I've owned this version of Windows, and it's just too much trouble.    ----Plus working 3rd shift now it makes it difficult to get anything done...... Sad )
I'm going to wait until I get a new computer.

Well, thanks for all of your help guys.

And, I'm sure I'll have more questions in the future....lol.

Rick
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (217 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!