Solved

java package issue

Posted on 2014-01-10
11
684 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now