Results 1 to 18 of 18

Thread: Java program

  1. #1
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Java program

    Hi.Before i 'm sorry if i will make mistake i dont speak very good english.One week ago i bought a book.Java from 0 to expert.First time i stay a lot for enviroment variables.After i did i tried some simple code and it's work.Now i tried to make a package with 2 class.When i compile with javac it's work but after i got one error.And i saw after the other programs that was good in one time now doesnt want to work anymore.

    I can't understand what it's wrong i did all the step from the book.
    https://s15.postimg.org/br7n6fw3v/image.jpgThe main source after compile and the eror
    https://s13.postimg.org/nudslovh3/image.jpgthe package with the class and after compile
    It must show on the cm after compile:
    "Suntem in ClasaUnu"
    "Suntem in ClasaDoi'
    enviroment variables that i entroduce are:
    user variables for Admin:
    classpath:->C:\Program Files\Java\jre\lib\rt.jar;.
    path:->C:\Program Files\Java\jdk\bin;.

    System variables
    CLASSPATH :->C:\Program Files\Java\jdk;C:\Program Files\Java\jre;.
    Path :-> C:\WINDOWS;C:\WINDOWS\SYSTEM;C:\Program Files\Java\jdk\bin;C:\Program Files\Java\jre\bin
    I search a lto and i didn't find if somebody want you can enter in my PC with team viewer.Please help me somebody with this error.Thanks for everything and sorry for my bad english.

  2. #2
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Java program

    I suggest you to use eclipse as your code editor, and compiler. Its much easier to see the code, has auto completion and so on... And you would see the line number of the error. Instead of using screenshots, its much better to actually copy paste the code here for every file.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    Quote Originally Posted by theunlockeRRR View Post
    I suggest you to use eclipse as your code editor, and compiler. Its much easier to see the code, has auto completion and so on... And you would see the line number of the error. Instead of using screenshots, its much better to actually copy paste the code here for every file.
    In general it is recommended not using an IDE until you've got through a few simple programs using the command line.
    This lets you learn how the classpath works and how Java views the world, for the compiler and runtime.

    For the OP, George, do not use environment variables outside of script files.
    That sets a global classpath for all your code, which is rarely (if ever) a sensible option.
    So get rid of those.

    Next, we need to see the error here, and associated code.
    Not links to images.
    Copy and paste them please.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Java program

    As already advised, please post the code here. Before that, go through BB Code List - Java Programming Forum - Learn Java Programming

    If your code is large, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    Hi sorry because i was late.
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Admin\Desktop>cd JAVA PROGRAMS

    C:\Users\Admin\Desktop\JAVA PROGRAMS>java AplicatieUnu
    Aplicatie independenta

    C:\Users\Admin\Desktop\JAVA PROGRAMS>javac -cp C:\Temp TestPachet.java

    C:\Users\Admin\Desktop\JAVA PROGRAMS>java -cp C:\Temp TestPachet
    Error: Could not find or load main class TestPachet

    C:\Users\Admin\Desktop\JAVA PROGRAMS>


    SO now it run the program but still when i compile test pachet didn't work should i delete all varyables from system?
    MY CODE FOR TEST PACHET is
    TestPachet.java code:>
    import pachet.exemplu.*;
    public class TestPachet{
    public static void main(String[] args)
    {
    ClasaUnu cu=new ClasaUnu();
    ClasaDoi cad=new ClasaDoi();

    }
    }

    THe package with the two class
    ClasaUnu.java location C:\Temp\pachet\exemplu code :>package pachet.exemplu;
    public class ClasaUnu{
    public ClasaUnu(){
    System.out.println("Suntem in ClasaUnu");
    }
    }

    COde for ClasaDoi.java location C:\Temp\pachet\exemplu code >
    package pachet.exemplu;
    public class ClasaDoi{
    public ClasaDoi(){
    System.out.println("Suntem in ClasaDoi");
    }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    Where is TestPachet.java?
    Is that in C:\Temp?
    Actually, since it compiles then I guess it is.

    What is in the Java PROGRAMS directory?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    So the ClasaUnu.java and ClasaDoi.java are C:\Temp\pachet\exemplu and in the JAVA PROGRAMS is TestPachet.java

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    OK, this comes down to classpaths.

    For the javac command, the classpath given by -cp is used to find classes that the file you want to compile needs.

    For the java command, the classpath given by -cp is used to find all classes, including the one you are using as your entrypoint (the one with main).

    So, in your case, you need to add . to the classpath:
    java -cp .;C:\Temp TestPachet

    This tells the JVM to also look in the current directory for classes, which in this case means your TestPachet one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    Ohhhhh finnaly it work thanks a lot!!!!!!!!!!!!In the book doesn't show us to put point "." .I have a question... WHy in the book it's only C:\Temp you can't put all the path C:\Temp\pachet\exemplu ?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    It's C:\Temp because that is the root of your source code.
    The "pachet\exemplu" part is the package structure, so part of the full class name.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    Sorry i was in the area and i didn't have internet.So pachet\exemplu is part of the package if my package had another name the folder must also have another name i mean if the package name was pack the folder must be named pack ? I undestand pachet\exemplu is part from the package structure but the name of the package is pachet.exemplu the folder name where is it shoudn't be pachet.exemplu?

  13. #13
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    I delete classpath from enviroment variables and i have only path with bin from jdk and jre is good ?
    C:\WINDOWS;C:\WINDOWS\SYSTEM;C:\Program Files\Java\jdk\bin;C:\Program Files\Java\jre\bin

  14. #14
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    I tried to make another package with one class
    Aplic.java code is package pac.test;
    public class Aplic{
    public Aplic()
    {int a=3,b=4;
    System.out.println(a+b);
    }
    }
    location is C:\Pac\Test

    and a java aplication Pachet
    import test.*;
    public class Pachet{
    public static void main (String[] args)
    {
    new Pachet();
    }
    }
    C:\Users\Admin\Desktop\JAVA PROGRAMS\Mine>javac -cp C:\Pac Pachet.java
    Pachet.java:1: error: package test does not exist
    import test.*;
    ^
    1 error

    C:\Users\Admin\Desktop\JAVA PROGRAMS\Mine>
    What is wrong now?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    Folder structure should reflect the package name, with sub-package names being sub-folders.

    Your PATH looks fine, assuming you can actually access java and javac.

    Please wrap you code in code tags, as mentioned in the link Darryl gave back in post #4.
    It makes the code easier to read as it retains its formatting.

    Your problem looks like a case issue. Java is case-sensitive.
    Test != test
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    George123 is offline Member
    Join Date
    Aug 2016
    Posts
    8
    Rep Power
    0

    Default Re: Java program

    I really don't know how to post like that i'm so sorry.....I have a big problem in this book the next chapter is instance: What is an instance here is pretty hard to understand .AN instance is like you give to a variable a number like x=2 and y=3 solve x+y?

    I try to compile the code to see the program how it work but i have an error.
    so i made a program and a class
    class Exemplu{
    int x;
    Exemplu(){
    System.out.println("constructor fara parametri");
    }
    Exemplu(int x){
    this.x=x;
    }
    static Exemplu primulX=new Exemplu(1);
    public void setX(int X)
    {this.x=x;
    }
    public String toString()
    {
    return "x="+x;
    }}

    and the program DiferiteInstante

    code>public class DiferiteInstante {
    public static void main (String [] args)
    {
    System.out.println("primulX:"+Exemplu.primulX);

    Exemplu obiectDoi=null;

    try
    {
    obiectDoi=(exemplu)
    Class.forName("Exemplu").newInstance();
    System.out.println("obiectDoi:"+obiectDoi);
    obiectDoi.setX(2);
    System.err.println("obiectDoi:"+obiectDoi);
    }
    catch(Exception e)
    {System.out.println(e);
    }
    Exemplu obiectTrei=new Exemplu(3);
    System.out.println("obiectTrei:"+obiectTrei);
    }
    }


    It said on the screen must show:
    primulX: x=1
    constructor fara parametri
    obiectDoi:x=0
    obiectDoi:x=2
    obiectTrei:x=3

    when i compile
    C:\Users\Admin\Desktop\JAVA PROGRAMS>javac Exemplu.java

    C:\Users\Admin\Desktop\JAVA PROGRAMS>javac Exemplu.java

    C:\Users\Admin\Desktop\JAVA PROGRAMS>javac DiferiteInstante.java
    DiferiteInstante.java:10: error: cannot find symbol
    obiectDoi=(exemplu)
    ^
    symbol: class exemplu
    location: class DiferiteInstante
    1 error

    C:\Users\Admin\Desktop\JAVA PROGRAMS>

    Also what is mean catch(Exception e)
    and why is put try{ obiectDoi=exemplu}
    i can't understand this code in the book doesn't said anything.
    I know i am anoying and i don't speak very good english but please help me i really want to learn java and im stuck sometimes... i can't put on my romain forum because are very very week i tried with easier programs and still didn't answer.Please help me and thanks a lot...
    Last edited by George123; 08-24-2016 at 09:03 PM.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Java program

    Quote Originally Posted by Tolls View Post
    Please wrap your code in code tags, as mentioned in the link Darryl gave back in post #4.
    It makes the code easier to read as it retains its formatting.
    Quote Originally Posted by George123 View Post
    I know i am anoying
    Yes, you are.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java program

    This is the link Darryl posted:
    BB Code List - Java Programming Forum - Learn Java Programming
    Read that bit and you will know how to use code tags.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 19
    Last Post: 07-17-2015, 03:40 PM
  2. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  3. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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