Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

java package issue

Posted on 2014-01-10
11
698 Views
1 Endorsement
Last Modified: 2014-02-07
Hi,
I am running below example
http://www.avajava.com/tutorials/lessons/what-is-the-transient-keyword-used-for.html
I created the TransientDemo.java file under below folder


C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff  


The TransientDemo.java file has package called cakes.

package cakes;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientDemo {

  public static void main(String[] args) {




I opened command prompt in my windows laptop went to below folder
C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff

so now I gave compile below command
javac TransientDemo.java  (i see class file generated at C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff)
and run command at same location (C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff)
java TransientDemo
I got error message saying the class not found.



when I  comment the package statement everything works perfect.



//package cakes;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientDemo {

  public static void main(String[] args) {



so now again I gave compile below command
javac TransientDemo.java  (i see class file generated at C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff)
and run command at same location (C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff)
java TransientDemo
I got output without error.

Can you please advise what is the issue with package statement.


what is meaning of  file extension of mydemo.ser.
what are practical uses of the transient

 please advise
Any links resources ideas highly appreciated. Thanks in advance
1
Comment
Question by:gudii9
  • 8
  • 3
11 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 39773607
If you define a package then your class should be within this package. So the class file generated at C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff should actually be in C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff\cakes. You can solve it by doing:

javac TransientDemo.java -d .
java cakes.TransientDemo

The first statement tells the compiler to output the class in the current folder, so you will see a new folder called "cakes" created, and inside it there is your class.

The second statement tells java to find the class called TransientDemo in the cakes folder.

what is meaning of  file extension of mydemo.ser

This is the serialised version of your output.

what are practical uses of the transient

If you don't want to serialise some fields you define them as transient.
1
 
LVL 7

Author Comment

by:gudii9
ID: 39786467
>>java cakes.TransientDemo
you mean below statement instead of above to compile it
javac cakes.TransientDemo.java

Or you mean the run statement itself when you say second statement as below.


>>The second statement tells java to find the class called TransientDemo in the cakes folder.

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39786475
If cakes folder is not there then does javac command creates it for us or just complains saying cakes folder is not there or it simply puts class file at C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff

Please advise
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 39786501
when i gave

javac TransientDemo.java -d .


it created
cakes folder under
C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff



then
it created two class files as below and put them under C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff\cakes
TransientDemo.class
TransientDemo$1MyDemo.class

what is the purpose of TransientDemo$1MyDemo.class


Also what is the use of -d . as in the command
javac TransientDemo.java -d .

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39786528
instead of javac TransientDemo.java -d .

when i gave
javac TransientDemo.java  

it did not give error. Instead it created  class file in folder below C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff
which i feel wrong. I wonder why it happened like that. please advise
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 500 total points
ID: 39789412
If cakes folder is not there then does javac command creates it for us

Yes.

what is the purpose of TransientDemo$1MyDemo.class

It means that you have another class called MyDemo inside the TransientDemo class.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39816144
If cakes folder is not there then does javac command creates it for us

Yes.


But in below case it did not create cake folder. Why?



instead of javac TransientDemo.java -d .

when i gave
javac TransientDemo.java  

it did not give error. Instead it created  class file in folder below C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff
which i feel wrong. I wonder why it happened like that. please advise

what is the significance of  '-d ' at end as below
javac TransientDemo.java -d .
0
 
LVL 7

Author Comment

by:gudii9
ID: 39816231
It means that you have another class called MyDemo inside the TransientDemo class.
I see inner class as belw but i wonder what is the purpose or practial use of inner class here.
public class TransientDemo {

      public static void main(String[] args) {

            class MyDemo implements Serializable {
                  private static final long serialVersionUID = 1L;

Please advise
0
 
LVL 35

Expert Comment

by:girionis
ID: 39817849
The -d means "destination", it is the destination of the compiled classes.

For your second question, read an extensive answer at javaworld and stackoverflow.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39819090

If cakes folder is not there then does javac command creates it for us

Yes

Does it create cakes folder in which of below scenarios (with or without -d.)
Scenario A: javac TransientDemo.java  


or

Scenario B : javac TransientDemo.java -d .


i am assuming below flow

I created the TransientDemo.java file under below folder


C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff  


The TransientDemo.java file has package called cakes.

package cakes;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientDemo {

  public static void main(String[] args) {




I opened command prompt in my windows laptop went to below folder
C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff

0
 
LVL 7

Author Comment

by:gudii9
ID: 39824483
so what happens when

I created the TransientDemo.java file under below folder


C:\Users\Desktop\gpfolder\gpmy\SampleCode\Online\avajava\stuff  


The TransientDemo.java file has package called cakes.

package cakes;(here with or without package statement cakes)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientDemo {

  public static void main(String[] args) {




also javac command with or without -d

javac TransientDemo.java -d .


javac TransientDemo.java

In what case cakes folder created with which javac command. please advise
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question