Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!

Lin Ahmad

Greenhorn
+ Follow
since Nov 30, 2014
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lin Ahmad

Now, I know how to add the class file!

I extracted the original JAR file, removed the META-INF directory, the MANIFEST.MF file and the xxx.class from this original JAR file. I then recreated the JAR by adding a new xxx.class and a new MANIFEST.txt file that I had created using the original MANIFEST.MF file. The command:


The jar command automatically creates the META-INF directory and puts the MANIFEST.MF file in.

This page provides me the answer: https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html

Thanks to all, appreciate your help :-)

Lin
Thanks for all your prompt replies, and for the link to the tutorial.

The JAR file does not have any source files, and has only class files. I've the source file for this file that I am trying to update. I updated this source file [xxx.java], and compiled it [xxx.class], and then updated the JAR file with this:

C:\Tivoli\batch\tst>jar uf Test.jar packagePath1\packagePath2\packagePath3\..\..\..\xxx.class

The package path: packagePath1\packagePath2\packagePath3\..\..\..\ to the xxx.class file is the same as the directory path of the xxx.class file in the JAR file.

The above jar command ran good - no error.

Question: How do I know that the class file was changed? After I ran "jar tfv Test.jar", the time on the class file did not change, only the time of the JAR file changed - it was updated to today's date (1/25).
I've a JAR file (which I will call the original JAR file) which includes some java classes and some other jar files. I need to extract one of the java classes to make some changes to it and put it back into the original JAR file.

I am thinking to de-compile the original JAR file using a Java De-compiler, and extract this java class. This will get me the java file. After making the changes for this java file, I'll recompile this java file, and will add the class file back to the original JAR file.

Questions:

1. Am I doing it right to de-compile the original JAR file just to get the java file I need?

2. If so, I need to re-archive the original JAR file with this newly updated java file, how do I do that? Is there a tool to re-archive?

Thanks in advance for your assistance.

Lin
Roel, thank you very much!

What I needed to understand was classpath (-cp)! I had read about classpath, but somehow just couldn't quite understand it completely. Your explanation makes it very clear.

When you execute the javac (and java) command, the current directory is used as the class path (if you don't use the -cp or -classpath option). The class path (as you probably know) is the location where the javac (and java) command has to look for other classes.



In my case, since it is only in the TestOtherJavaFiles directory that the Sludge class is visible, the javac command has to specify the parent directory in the classpath (..) if I am compiling from the book directory. If I am running the javac command from the TestOtherJavaFiles directory, then I do not need to worry about the classpath and can use this command:

javac book/Goo.java.



Appreciate your explanation of the relative path too. It is very clear.

Below are the successful runs!

# 1:
c:\TestOtherJavaFiles\book>javac -cp .. Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:44 PM <DIR> .
01/02/2016 08:44 PM <DIR> ..
01/02/2016 08:44 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free

# 2:
c:\TestOtherJavaFiles\book>javac -cp .;.. Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:42 PM <DIR> .
01/02/2016 08:42 PM <DIR> ..
01/02/2016 08:42 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free

# 3:
c:\TestOtherJavaFiles\book>javac -cp c:\TestOtherJavaFiles Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:45 PM <DIR> .
01/02/2016 08:45 PM <DIR> ..
01/02/2016 08:45 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free


Roel & Felipe: Also, I was able to execute the Goo class by making sure to use the forward slash - / and not to put the .class at the end of the java command. Thanks!

c:\TestOtherJavaFiles>java book\Goo
Error: Could not find or load main class book\Goo

c:\TestOtherJavaFiles>java book/Goo
sludge

My sincere thanks! Yeah! My first cow

Lin
Hi, can someone please help with the three questions below? Thanks much in advance!

Book: OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)
Part I OCA and OCP > Declare Class Members > Access Modifiers: Section: Public Members

Below two source files are listed: class Goo in package book, and class Sludge in package cert.

Sludge.java:


Goo.java:


I created a folder C:\TestOtherJavaFiles and then created two subfolders: book and cert.

Created and saved Sludge.java in the cert folder; compiled this class – no issue with this.

Created and saved Goo.java in the book folder - see below;

C:\TestOtherJavaFiles\book

Directory of c:\TestOtherJavaFiles\book

01/01/2016 01:32 PM <DIR> .
01/01/2016 01:32 PM <DIR> ..
01/01/2016 01:23 PM 150 Goo.java
1 File(s) 150 bytes
2 Dir(s) 422,746,611,712 bytes free


Unable to compile the Goo.java in the book folder; the error:

c:\TestOtherJavaFiles\book>javac Goo.java
Goo.java:3: error: package cert does not exist
import cert.*;
^
Goo.java:8: error: cannot find symbol
Sludge o = new Sludge();
^
symbol: class Sludge
location: class Goo
Goo.java:8: error: cannot find symbol
Sludge o = new Sludge();
^
symbol: class Sludge
location: class Goo
3 errors

Ques # 1: It seems that the Goo.java file in the book package is unable to see the other package, cert. As such the javac command should not be run in the book folder, am I right?


I moved back up one folder, to the TestOtherJavaFiles folder, and compiled Goo.java using the command:

c:\TestOtherJavaFiles>javac book\Goo.java. This time it compiled. Below is the class file:

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/01/2016 01:43 PM <DIR> .
01/01/2016 01:43 PM <DIR> ..
01/01/2016 01:43 PM 317 Goo.class
01/01/2016 01:23 PM 150 Goo.java
2 File(s) 467 bytes
2 Dir(s) 422,746,374,144 bytes free


Ques # 2: The compilation worked because at the level of the TestOtherJavaFiles folder, both the cert and the book folders are visible?


Ques # 3:
I tried to execute Goo.class (this has the main method) in the three directories below, none worked. It could not find the main class. Why?


c:\TestOtherJavaFiles\book>java Goo.class
Error: Could not find or load main class Goo.class

c:\TestOtherJavaFiles\book>cd ..

c:\TestOtherJavaFiles>java book\Goo.class
Error: Could not find or load main class book\Goo.class

c:\TestOtherJavaFiles>java Goo.class
Error: Could not find or load main class Goo.class

I did some searches and followed this: http://www.coderanch.com/t/416708/java/java/set-Java-Classpath#1835940

Thanks, appreciate your assistance.

Lina@783
Thanks Les!

1. Yes, it boils down to the business rules .. some serious thoughts need to be considered.

2. We used our JPA Generator (in-house) to generate the entities for the old System, so I gather we'll re-run the JPA Generator for a specific entity of the old System if we need to add more columns to the associated table. Do you know if this is usually done this way when adding more columns to an existing table?

3. Agree, we will use our in-house JPA Generator to generate entities for the new System.

Regards,
lina
We have an application, which I’ll call the old system. We are creating a new application and will be using some of the data from the same database that the old system is using.

I am thinking we can add new tables or add columns to existing tables in the old system’s Database.

Question:
1. Can I do that? Two systems using the same database?
2. We are using Java EE with EJB3. If we can just add new tables or add columns to existing tables to the old system’s Database, do we have to reverse engineer the models (entities) for the old system, or do we just run the JPA Generator again to get the models for the old system?
3. For the new system, how do I generate the entities?

Thanks in advance for your input!

lina
Thanks! Matthew. You're right it is throwing a ClassCastException.



Also, the check below produces the child class:


The child class is an association table with two fields: childID and parentID. The parent class has the parentID as the primary key and about 40 other fields. Modifications to these two entity classes are not allowed.

Lin
I have a child class which extends a parent class.

@Entity
public class ChildClass extends ParentClass implements Serializable

When I retrieve the child class (see below), I get an instance of the parent class even though the 1st parameter is the child class:
ChildClass childClass = em.find(ChildClass.class, childClassId);

1. I thought I am supposed to get an instance of the child class, not an instance of the parent class. Can someone explain why this is so?
2. How do I get an instance of the child class?

Thanks,
Lin