Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java package issue

Posted on 2014-01-10
11
Medium Priority
?
720 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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