www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: Troubleshooting Guide For Some Basic Java Problems.

Hybrid View

  1. #1
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123

    Troubleshooting Guide For Some Basic Java Problems.


    Troubleshooting Guide For Some Basic Java Problems.


    Step One

    The first and fore most requirement is the installation of the required(or most current j2sdk, the newer versions of jdk are known as J2SE) from java.sun.com.
    if you have done that then you will have a new folder created in the C:\ drive.
    Typically it will be something along the following format
    C:\j2sdkxxxxx (I think jdk 5 is installed in C:\Program Files\ folders?)
    where xxxxx will be the version number for the current j2sdk you installed. At this point on my machine it looks like this
    C:\j2sdk1.4.1_05


    Step Two

    Next step is setting up the path and class paths for your system so that windows knows what to do with commands like
    javac or java.
    Lets see what happens if we want to test our setup as it is right now.
    on your windows machine go to
    bottom left corner and click on
    Start>Run
    in the text field type in
    cmd
    if you are one of those who are still on win98/95 or win me then you will need to type in
    command

    This will bring up a dos window. in the dos window at the prompt type in the following
    javac

    At this point we are expecting the following or a message like this

    'javac' is not recognized as an internal or external command
    operable program or batch file.


    This is due to the reason that windows machine does not know as yet that what to do with "javac" command which essentially
    is required to compile java src code.Close the does window,because windows will not apply new variable changes in this session,
    you will have to open up a new window once you are all set with variable settings.


    Step Three

    Lets go and set the environment variable.On machines with win2k or newer systems go to
    Start>Settings>Control Panel>
    Locate and click on the "System" icon.
    This will bring up "System Properties" window.
    by default "General" is selected, locate and click on "Advanced" tab.
    Locate and click on the button that has the following caption on it.

    Environment Variables


    This will bring up the a window with 2 sections in it, top one is for the user as whom you have logged in,and the lower
    section is for system.
    We will work in the user variables section.

    You will see 3 buttons right below the display window in the "user variables" section,

    New, Edit and Delete.

    we want to create home variable for java.Click on New button.
    It will bring up a small window with 2 text fields
    Variable name: _______________________
    in variable name text field type the following
    JAVA_HOME

    Variable value:______________________
    in the variable value field type the folder name + path where on C drive you have j2sdk installed.
    in my machine this value looks like this
    C:\j2sdk1.4.1_05
    If you look in the window you will see the following record added which you have just added

    Variable Value

    JAVA_HOME C:\j2sdk1.4.1_05

    of course there may be others too.We are half way thru..:-)


    Step Four

    Now lets set the path to required java bin files to run several java commands.
    Look in the window for the user variables and try to locate a Variable name Path
    if you see one then we need to edit and add new values,if you don't see one then we need to create one.
    And then add values.
    First lets create one.

    You will see 3 buttons right below the display window in the "user variables" section,

    New, Edit and Delete.

    we want to create home variable for java.Click on New button.
    It will bring up a small window with 2 text fields
    Variable name: _______________________
    in variable name text field type the following
    PATH

    Variable value:______________________
    in the variable value field type the folder name + path where on C drive you have j2sdk installed.
    in my machine this value looks like this
    %PATH%;%JAVA_HOME%\bin;%JAVA_HOME%\lib;
    If you look in the window you will see the following record added which you have just added.You will see that JAVA_HOME is
    resolved to its actual path on the disk, again on my machine it looks like below.

    Variable Value

    PATH C:\j2sdk1.4.1_05\bin;C:\j2sdk1.4.1_05\lib;

    Click "Ok" at the bottom of window, and then click on again.
    Now lets repeat Step Two here again.
    This time around(provided everything was done exactly as I mentioned above) when you type in javac at the command
    prompt you should get the following message

    C:\Documents and Settings\Khalid.Ali>javac
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are used
    -classpath <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -help Print a synopsis of standard options

    if we see the above message,we are all set for some serious java coding..:-).


    Compilation and Running of Java Code

    Open note pad, and copy and paste the following code in it
    Code:
      public class HelloWorld{
     	public static void main(String[] args){
       		//declare and define a variable of type String
       		String helloWorld = "Hello Java World! Here I come.";
       
    		//Now print it out on screen
    		System.out.println(helloWorld);
     	}
      }
    There are couple of little precautions you will need to take.Make sure that file name is HelloWorld.java, class name must always
    match the file name.If everything is fine and dandy, nothing will happen and you will be brought back to the dos prompt.

    C:\development\java>javac HelloWorld.java

    C:\development\java>


    In this case now is the time to run the newly written java application.if you want to make sure,just look in the directory where you have
    this file,now there is another file with the same name but different extension.
    HelloWorld.class

    java src files always have *.java extension and compiled classes have *.class extension.
    Anyways, let run this app, type in the following at the dos prompt.

    C:\development\java>java HelloWorld
    Hello Java World! Here I come.

    C:\development\java>

    The java runtime engine will print the value of the variable then come to dos prompt for a new command.
    At this point typically a beginner may see the following error.

    C:\development\java>java HellowWorld
    Exception in thread "main" java.lang.NoClassDefFoundError: HellowWorld

    as you can see it makes total sense that its not finding the class name, because HelloWorld has a "w" in it,once the class name is
    corrected it will run.
    If you have class name that's different then the file name then you will get the following message.

    C:\development\java>javac HelloWorld.java
    HelloWorld.java:1: class HellowWorld is public, should be declared in a file named HellowWorld.java
    public class HellowWorld{
    ^
    1 error

    C:\development\java>
    Obvious enough...I'll just go take a look at my class name and remove the w from so that its the same as the file name
    HelloWorld.java....

    I hope this helps, I'd probably add more stuff as it comes to mind...
    Edit:Sep 06, 2005
    MySQL' centric jdk settings
    Code:
    Following are the settings for MySQL to work on my windows 2000
    work station. Make sure you have same settings on your machines
    for MySQL to work.
    
    JDK installed at the following address
    C:\j2sdk1.4.2
    
    Environment variable for java home set as follows
    JAVA_HOME=C:\j2sdk1.4.2
    
    Paths set to point to java home as follows
    Path=C:\j2sdk1.4.2\bin;C:\j2sdk1.4.2\lib;
    
    Mysql driver is placed at the following location
    C:\Program Files\Java\j2re1.4.2\lib\mysql-connector-java-3.1.0-alpha-bin.jar
    
    I have attached a zip file that contains a DbTest.java class to test the
    connection.
    Deploying a Servlet on Tomcat

    The tutorial above is mostly the works for any servlet on any server, only difference there will be that the root folders names could be different in different server environments.
    Sometimes people face problems in their JSP pages that their user created objects/classes throw an error that cannot resolve symbol. This error is almost always caused by the fact that there is a problem in the setup and the classes are not at right place for the jsp page to find them. Deploying a Servlet on Tomcat--> this tutorial will actually guide you through the process of setting up a correct context for any new webapplication as well as where required files should be at.
    Attached Files Attached Files
    Last edited by Khalid Ali; 03-27-2006 at 09:48 PM.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources
    Share on Google+

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Thanks Khalid, this is great. One question, though, if I installed the complete kit downloadable at <http://java.sun.com/j2ee/1.4/download.html#sdk>, and installed it in C:\java, what would I use for my JAVA_HOME and PATH global variables? I tried using C:\java instead of C:\jdkxxxx, but it didn't work...

    Thanks!
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
    Share on Google+

  3. #3
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Never mind, I found it in: C:\java\jdk\bin. Thanks.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
    Share on Google+

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    Originally posted by Jona
    Never mind, I found it in: C:\java\jdk\bin. Thanks.
    Glad that it helps, I remember quite a few years ago when I started learning java, the biggest darn problem I had was to set up the env variables on my windows 95 box....anyways...looking forward to answer some questions....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources
    Share on Google+

  5. #5
    Join Date
    Feb 2005
    Location
    California
    Posts
    2
    Ohhh my...had I only had this a week ago.

    I hope other newbies read this first...it will save them many hours of frustration.
    New Programmer
    Share on Google+

  6. #6
    Join Date
    Oct 2004
    Location
    Behind you
    Posts
    32
    There's a handy program that i use now, it does all that stuff in here automatically. It does take quite much memory but that's not too bad.

    For windows only:
    Eclipse

    Hope it helps

    Grtz,

    Entertainer
    Share on Google+

  7. #7
    Join Date
    Sep 2004
    Posts
    80
    There are couple of little precautions you will need to take.Make sure that file name is HelloWorld.java, calls name must always
    match the file name.
    Oops! Typo!
    Fix:
    There are couple of little precautions you will need to take.Make sure that file name is HelloWorld.java, class name must always
    match the file name.
    Share on Google+

  8. #8
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    4

    Exclamation

    FWIW: I just solved one little problem that prevented me from running a java app that compiled without error and contained no logic errors as it displayed simple text.

    On a Windows XP computer, I have Oracle tools installed for managing a couple of remote instances. Its installation had added JRE version 1.3.1 that comes with Oracle Admin tools. The JDK I downloaded and installed is a more recent version, C:\Program Files\Java\j2re1.4.2_08. The compiled class could not be run with the older JRE.

    When I checked
    Code:
    c:\apps\java -version
    against
    Code:
    c:\apps\javac -version
    , I got two different versions -- of course!

    Simple solution: changed the PATH env variable from c:\Program Files\Oracle\jre\...\bin to c:\[folder of new JRE]\bin

    General Rule: if you're getting version errors after an app compiles correctly, check your PATH for other JREs.

    HTH someone else.

    Cheers.
    S.Larkin
    Share on Google+

  9. #9
    Join Date
    May 2004
    Location
    The Big M
    Posts
    669
    Wow this helped so much! Thanks Khalid Ali! Seriously, this is like the only place on the web to fix the class path problem!
    <? Damn Browser Incompatibilities ?>
    Dell 1505~2.0 Core Duo~1 gig DDR2-533~ Radeon x1400(256mb)~ Hitachi 100gb 7200rpm
    <? Go Blue. ?>
    Share on Google+

  10. #10
    Join Date
    Sep 2005
    Posts
    12

    Still not working

    I typed the following for Name and Value

    Java_Home Java_Home C:\J2sdk-1_3_1_16
    PATH C:\J2sdk-1_3_1_16\bin;C:\J2sdk-1_3_1_16\lib;

    This is not working.

    And I have windows xp
    Share on Google+

  11. #11
    Join Date
    Dec 2005
    Posts
    16
    Yah i typed the following: C:\jdk1.5.0_06/bin;\C:\jdk1.5.0_06\lib;

    and it's not working for me as well it still comes up with the: 'javac' is not recognized as an internal or external command
    operable program or batch file.

    It's not workin for me too and i also have xp. Did i type somethin wrong or what?
    Share on Google+

  12. #12
    Join Date
    Dec 2005
    Posts
    16
    Ok I have been working on getting it running for a while now and it still won't work. What is up with my damn computer?
    Share on Google+

  13. #13
    Join Date
    Jan 2006
    Posts
    1
    "What is up with my damn computer?"

    Calm down dude, the path u r entering doesnt seem fine to me. is this a typo or u r actually entering a wrong path?

    replace the line C:\jdk1.5.0_06/bin;\C:\jdk1.5.0_06\lib; with
    C:\jdk1.5.0_06\bin;C:\jdk1.5.0_06\lib;

    hopefully it would help. chao.
    Share on Google+

  14. #14
    Join Date
    Jan 2006
    Posts
    1

    Unhappy not working

    hey khalid
    thanks for you guide on how to install java and get it to work...but unfortunately, i still cant get mine to run.
    i have got installed jdk1.3.1_16 in my c drive and also sun/Appserver which is J2EE

    i am not sure which one to work with or why have many versions.
    please advice!!
    thanks
    Share on Google+

  15. #15
    Join Date
    Feb 2006
    Location
    China
    Posts
    72
    yes, I have do this and it works.

    thanks your advice.
    Share on Google+

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles