Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm completely new to Java but I wa

Status
Not open for further replies.

rcusr

Technical User
Sep 4, 2002
26
0
0
GB
I'm completely new to Java but I was working through a tutorial.


Firstly, I created four class files... I then used command prompt to drill down to the folder I had saved the class files in. I then typed the command: java checklinks


It came back with this error:

C:\Documents and Settings\rcooper\My Documents\_FWIL\_linkchecker>java checklink
s
Exception in thread "main" java.lang.ClassFormatError: checklinks (Bad magic num
ber)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


Does anyone know why this is happening?


Richard
 
I think I've narrowed down the problem... The way I created the 4 classes was using a text editor and then just saved as .class

I figured out now that I have to use Dos to recompile my class files with:

javac *.java


But I now get the error:
'javac' is not recognized as an internal or external command,
operable program or batch file.


Does that mean that my Java SDK is not installed/installing correclty?
 
try add the java path to the path stmt(under control panel on NT),i.e d:\java\bin. You may have to reboot your machine.
Then run cmd.exe and type in javac. it should lists java with a bunch of lines on how to use the syntax
 
yes, when compiling java file, you need to use this syntax:-

javac javafile.java

when running it:-

java javafile(case sensitive). Else, the VM will not be too happy about it :)
 
You are 50% there, follow the below installation instructions. Please us know if you are having any problems doing the below.

For more infomation see:

Update the PATH variable
You can run the Java 2 SDK without setting the PATH variable, or you can optionally set it as a convenience.
Should I set the PATH variable?
Set the PATH variable if you want to be able to conveniently run the Java 2 SDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:> \j2sdk1.4.1_<version number>\bin\javac MyClass.java

It's useful to set the PATH permanently so it will persist after rebooting.
How do I set the PATH permanently?
To set the PATH permanently, add the full path of the j2sdk1.4.1_<version number>\bin directory to the PATH variable. Typically this full path looks something like C:\j2sdk1.4.1_<version number>\bin. Set the PATH as follows, according to whether you are on Microsoft Windows NT or 98/2000/ME.

Microsoft Windows NT, 2000, and XP - To set the PATH permanently:


Choose Start, Settings, Control Panel, and double-click System. On Microsoft Windows NT, select the Environment tab; on Microsoft Windows 2000 select the Advanced tab and then Environment Variables. Look for &quot;Path&quot; in the User Variables and System Variables. If you're not sure where to add the path, add it to the right end of the &quot;Path&quot; in the User Variables. A typical value for PATH is:
C:\j2sdk1.4.1_<version number>\bin

Capitalization doesn't matter. Click &quot;Set&quot;, &quot;OK&quot; or &quot;Apply&quot;.
The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to j2sdk1.4.1_<version number>\bin.


The new path takes effect in each new Command Prompt window you open after setting the PATH variable.
Microsoft Windows 98 - To set the PATH permanently, open the AUTOEXEC.BAT file and add or change the PATH statement as follows:

Start the system editor. Choose &quot;Start&quot;, &quot;Run&quot; and enter sysedit, then click OK. The system editor starts up with several windows showing. Go to the window that is displaying AUTOEXEC.BAT

Look for the PATH statement. (If you don't have one, add one.) If you're not sure where to add the path, add it to the right end of the PATH. For example, in the following PATH statement, we have added the bin directory at the right end:

PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:
\J2SDK1.4.1_<version number>\BIN

Capitalization doesn't matter. The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows searches for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to j2sdk1.4.1_<version number>.

To make the path take effect in the current Command Prompt window, execute the following:
C:> c:\autoexec.bat

To find out the current value of your PATH, to see if it took effect, at the command prompt, type:
C:> path

Microsoft Windows ME - To set the PATH permanently:

From the start menu, choose programs, accessories, system tools, and system information. This brings up a window titled &quot;Microsoft Help and Support&quot;. From here, choose the tools menu, then select the system configuration utility. Click the environment tab, select PATH and press the edit button. Now add the SDK to your path as described in step b above. After you've added the location of the SDK to your PATH, save the changes and reboot your machine when prompted.
 
Brilliant, thanks very much to both of you, the path bit got the javac command recognised...

But unfortunatley it still won't compile the the java files. Can I check with you that I created the files correctly?

What I did was copy and paste some java code into a text editor. I then saved the file as a .java. Next I tried to compile the files using the javac command which gave me this error:

Spider.java:133: ')' expected
!connection.getContentType().toLowerCase().s
^
Spider.java:133: cannot resolve symbol
symbol : variable s
location: class java.lang.String
!connection.getContentType().toLowerCase().s
^
Spider.java:144: cannot resolve symbol
symbol : variable is
location: class Spider
Reader r = new InputStreamReader(is);
^
Note: CheckLinks.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
3 errors

The code I was using was from the 4 listing at the bottom of this page:
 
Slight exaggeration... I tried compiling the them separately and managed to get the following done:
ISpiderReportable.java
HTMLParse.java

I then did some trouble shooting and managed to get this one two compile into two classes?:
Spider.class
Spider$Parser.class

So now I'm struggling with the depreciated tag. I used the -deprecation function of the complier which resulted in this:

C:\Documents and Settings\rcooper\My Documents\_FWIL\_linkchecker>javac CheckLin
ks.java -deprecation
CheckLinks.java:188: warning: setLabel(java.lang.String) in javax.swing.Abstract
Button has been deprecated
begin.setLabel(&quot;Cancel&quot;);
^
1 warning


Am I using a version of SDK that is not new enough or too new? Version: j2sdk1.4.1_02
 
SDK 1.4 is the latest; the reason why that begin.setLabel
is not deprecated.

For instance, in multithread env, stop() is no longer a valid Thread object - it is deprecated in version 1.4.

In short, these commands are obsolete (not backward compatible)
 
Is there a command that I could use in its place or perhaps is it possible to run multiple versions of SDK?
 
note: prior to version 1.4, they're called JDK ;)

do this at the command prompt:-
javac

you will see -classpath <path>

hence, use it to set to the classpath of the particular JDK or SDK you want. I never tried it but for 1.3, it could be:-

c:\javac -classpath c:\java13 etc.


also, before running the java command, do this:-

C:\java -version

this will tell you what version of JVM you're using.

good luck


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top