Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 54

Thread: How to referenc a class file. using only notepad and run cmd.

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Which of the steps I asked you to do in post#20 were skipped or done differently than I suggested? For example I did not say to execute the Bootclasspath program. What else did you do?

    Restore the files and folders to the way they were before post #20 and re-do the steps in post#20.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Edit the FixChap5.java and add import Bootclasspath.*; to it
    compile and execute it. (the default classpath should be the folder with these three things in it: the FixChap5 files and the package1 folder.)
    Sorry Norm did that, what by the way is the third thing, you only mention the first two.

    Cheers.

    SpaceMonkey, off to work now.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    The three things in the test folder:
    FixChap5.java
    FixChap5.class
    package1 folder

    The package1 folder should have:
    Bootclasspath.java
    Bootclasspath.class
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    I have the FixChap5 class and java in the folder that holds the package1 folder.

    I have the Bootclasspath.java and class in the package 1 folder.

    This is still not compiling, and I do not see how it will because FixChap5 and Bootclasspath both need to be in the same base folder for this to work without a class path.

    Or possibly you are thinking that we need to put a:
     import Bootclasspath.*;
    Where as we need a:
    import package1.*;

    Which works.

    So if that is the case, next step.

    Thanks again.

    Space "Enjoying the sun on this sunny Saturday" Monkey

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    I assume the code compiles and executes now. It using the current directory (.) as the classpath.
    We'll change the exercise so you need an explict -classpath value to find the class file in the package1 package.

    Create a new folder: PathToPackage1 in the folder with the FixChap5 files.
    Move the package1 folder and its contents to that folder.
    You won't have to recompile the contents of the package1 folder.
    Change to the folder containing the FixChap5 files.
    Add a -classpath PathToPackage1 option to the javac command and compile the FixChap5 program.
    Add the same to the java command to execute the program.
    Last edited by Norm; May 12th, 2012 at 08:58 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:
    Not sure what you mean by:
    Add a -classpath PathToPackage1 option to the javac command.
    Do I type this in the Notepad where I typed the code or do I type this in the Command Prompt area, or the Environmental Variables area?

    Add the same to the java command to execute the program.
    Because I do not understand the first bit I am not sure where I am supposed to add the java command, and to be honest I think the java command is the -classpath PathToPackage1, but I am not sure.

    Thanks.

    Space Monkey

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    The javac and java commands are entered in a command prompt on commandlines. You add options to a command by putting them on the command line folowing the name of the command.
    For example:
    java -cp PathToPackage1 FixChap5
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    OK will try the above, this will not remove any other classpaths to, say the bootstrap classes?

    Also I like the place where you enter the environmental variables. So at some point later I would like to know how to enter the class path there.

    ACTUAL RESPONSE TO YOUR INSTRUCTIONS.

    I typed in
    java -cp PathToPackage1 FixChap5

    and got the error.

    Could not find or load main class FixChap5.

    Cheers.

    SpaceMonkey

  9. #34
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Looked online, found

    java -cp . PathToPackage1 FixChap5 (The apostrophe is added.)

    and that gave me the error:

    Could not find or load main class PathToPackage1.

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Yes, I left out the pointer to the current directory (.)

    Separate the two paths with a ; not a space:
    java -cp .;PathToPackage1 FixChap5

    Here the classpath points to the current folder where the FixChap5 class is located
    and to the PathToPackage1 folder where the package1 folder is.
    package1.Bootclasspath is a class referenced by FixChap5
    Last edited by Norm; May 12th, 2012 at 09:45 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    SPACE MONKEY (May 12th, 2012)

  12. #36
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    "-cp" is that short for "Current Path"?

    It works when I type that in, but how do I set the class path and not have to type that in, can I set it in the "Environmental Variables" ?

    Also lots of thanks, wanted to get my question in first. lol.
    Well it works. Thanks again.

    Also it the "." indicative of "this folder" or something like that?

    Space Monkey.

  13. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    -cp short for -classpath

    You won't want to set the ENVIRONMENT variable for every set of programs you write. Use the -cp option on the javac and java commands. Make a batch file with the commandline

    To execute the Bootclasspath program from the folder containing the PathToPackage1 folder:

    java -cp PathToPackage1 package1.Bootclasspath
    Last edited by Norm; May 12th, 2012 at 09:42 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #38
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    Sorry I was not clear,

    I want to know how to run the program FixChap5 without typing the the classpath. I want to set the class path some where so I do not have to type in:
    -cp .;PathToPackage1 FixChap5

    So ideal solutions would be:
    java FixChap5

    So that would entail setting the classpath for FixChap5 to find the class Bootclasspath automatically.

  15. #39
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    Y
    ou won't want to set the ENVIRONMENT variable for every set of programs you write.
    Why ?

    Now I do because I want to practice it so I do not forget it.

    Also is the notation different?

    My main problem with all of this was I did not know that I needed to put "java" before the -cp.

    I would never have worked that out.
    Last edited by SPACE MONKEY; May 12th, 2012 at 09:58 AM. Reason: Explaining the one thing I would nver have worked out.

  16. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Use a batch file with the full commandline to save entering the full classpath and other commandline options.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #41
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    When you say use a batch file. Do you mean in the batch file properties, or in the code in the FixChap5 or in the Bootclasspath, and if so what is the notation?

    Cheers.

    SM.

  18. #42
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    We do not enter FixChap5 anywhere. How can the compute know that that is the program we want to run?

  19. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    On windows a batch file's extension is .bat. I use them for executing test programs. You enter the name of the batchfile and windows reads it and executes it contents.
    Here's a sample:
    @Rem Setup for access to My HTTP Server - Run in a DOS prompt window
    @ECHO OFF
    %DEV_DRIVE%
    CD %DEV_HOME%\JavaDevelopment\
    Echo - - - - - - - - - -
    Echo Starting HTTP server - This window can be closed when 'Finished'
    Echo Remember to append :8080 to URL to reach this server!
    Java.exe -DDEV_DRIVE=%DEV_DRIVE% -DDEV_HOME=%DEV_HOME% -DDOCS_HOME=%DOCS_HOME% -cp %DEV_HOME%\Java\jsdk2.0\lib\jsdk.jar;%DEV_HOME%\Ja vaDevelopment\HTTPServer\;. HTTPServer.HTTPServerMain -p 8088 %1 %2 %3 %4 %5 %6
    MORE
    If you don't understand my answer, don't ignore it, ask a question.

  20. #44
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    Waaaaaa. What is that crazyness you printing in your quote up there. lol

    Where exactly do you enter this information. I am never sure if this is in the command line or in the program file.

    PLEASE ANSWER THIS QUESTION.
    How did the computer know we where working on FixChap5 file?

    Because the line java -cp .;PathToPackage package1.*;

    has not got Fix Chap5 in there anywhere.

    Thanks again

  21. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Create a batchfile that contains the full commandline you use to execute the java program.

    java -cp .;PathToPackage package1.*;
    Where did that line come from? That is not a valid commandline.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #46
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    So correct is; java -cp .;PathToPackage1 FixChap5 and thus the FixChap5 is at the end.

    If the FixChap5 class is not in the package "PathToPackage1" how can it call the Bootstrapclass method if the commandline telling it to call the method is written in a file already in the PathToPackage1 package. ?

    Are you saying to write a bit of code that prints out:
    java -cp .;PathToPackage1 FixChap5
    onto the command Prompt area that then builds the bridge between the two bits of code?

    Cheers.

    Space Monkey

  23. #47
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    java -cp .;PathToPackage1 package1.Bootclasspath

    The above bit of code has not FixChap5 in there anywhere, can you explain how it knows that FixChap5 is the calling program?

    Cheers.

    Space Monkey.

  24. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    java -cp .;PathToPackage1 package1.Bootclasspath
    The FixChap5 is not involved at all in the above.
    Look at the Bootclasspath program. It has a main() method that is being called by the java program.

    FixChap5 class is not in the package "PathToPackage1"
    There is no package named: PathToPackage1
    FixChap5 is not in any package.

    PathToPackage1 is the path from the current directory to the package1 directory.
    Bootclasspath is in the package1 package. That classpath value is how the java program finds the package1 package.

    Create a file named: FixChap5.bat in the folder with the FixChap5 files and put the following lines into it:
    java -cp .;PathToPackage1 FixChap5
    MORE

    Then double click on the file.
    Last edited by Norm; May 12th, 2012 at 11:07 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    SPACE MONKEY (May 12th, 2012)

  26. #49
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    Dear Norm:

    NICE

    What witchcraft is this!!!!.

    OK now that is what I was talking about this I can play around with.

    I am going to look up bat files and the rest of this.

    You have given me a good jumping off point, thanks.

    I will be back tomorrow, with results and questions.

    Many thanks.

    Space Monkey.

    P.S. I will mark this as answered sometime after tomorrow after I have come back with some interesting questions, of which I will have many.
    Last edited by SPACE MONKEY; May 12th, 2012 at 11:14 AM.

  27. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to referenc a class file. using only notepad and run cmd.

    I use frequently use batch files. Knowing how to write them will be useful for you.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    SPACE MONKEY (May 13th, 2012)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. making a notepad with java netbeans
    By tobioleye in forum Member Introductions
    Replies: 1
    Last Post: May 10th, 2011, 07:46 AM
  2. Ok, Notepad class has returned!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:50 PM
  3. Replies: 5
    Last Post: November 13th, 2010, 01:53 PM
  4. notepad as backend database
    By chinni in forum JDBC & Databases
    Replies: 0
    Last Post: October 21st, 2009, 01:48 AM