14 Replies - 469 Views - Last Post: 05 September 2012 - 01:42 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Editing and compiling a Java class

Posted 05 September 2012 - 10:29 AM

Hi all,

I have a group of Java utility classes that aren't in packages, and they each look roughly like this:

public class MyExample {
    // Code
}



Depending on where the class is needed, I want to give it a specific package name, like this:

package myExamplePackage;
public class MyExample {
    // Code
}



But, I want to add this package name and compile it without creating a temporary intermediate file. I'd like to do this as a one-liner as well, because this command will be in a Makefile and I don't think you can have loops in a Makefile.

What I have right now is:

sed '1i package myExamplePackage;' ../myUtilities/MyExample.java | javac



This almost works for a single file -- the sed works, but javac doesn't like the stream. Is there a way to get javac to like the stream and incorporate a loop here?

Or am I making things too complicated, and there's an easier way to do this altogether?

I'd appreciate any help.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Editing and compiling a Java class

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Editing and compiling a Java class

Posted 05 September 2012 - 10:56 AM

Wait, what? You want to temporarily include this in a package?

Why not just compile your utility classes to a jar and include the jar where you need it?
Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 11:23 AM

Thanks Jon,

You can do that, include one Jar in another? Can you create the Jar and include it in another Jar as a package?

Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Editing and compiling a Java class

Posted 05 September 2012 - 11:35 AM

You can put a .jar in a .jar
or if you want, and this is what I do, explode all your .jar in a temp directory and them jar that thmp directory

So if I have ca.pblinc.myapp to distribute and that application uses HSqlDD I
- unjar my app in a temp dir which creates \ca\pblinc\myapp\...
- un jar HSqlDB in a temp dir which creates \org\hsqldb\...

then jar the whole directory so the .jar contains \ca.pblinc... and \org.hsqldb...

I wrote a .bat that does that automatically
Was This Post Helpful? 0
  • +
  • -

#5 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 11:47 AM

Thanks pbl,

I'm not sure I understand what you're doing there, can you clarify please?

I don't need to make temporary directories, I just want to take some Java files that don't have a package in them, compile them, and be able to put them in any package I want. The only way I know how to do that is to edit the Java file before compilation to have the proper package name in it. Is that what you're doing in that example?

Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Editing and compiling a Java class

Posted 05 September 2012 - 11:59 AM

Okay, sorry, I shouldn't have brought jars into it, it's needless complication. Let's back up: you have a bunch of utility classes that you want to include in various projects.

What I'm asking is, why don't you just make those utility classes into a package, and include that in the projects where you need these classes?
This is what you do with all of the built in "utility classes" like java.util.ArrayList and so forth, except someone has already built those packages for you.
Was This Post Helpful? 0
  • +
  • -

#7 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:08 PM

Thanks Jon,

I understand what you're saying, but wouldn't that just cause the same problem?

Here's how things stand now in my code:
RootDir
   dir1
      images
      source
          menus
          drawing
             objects
             // utilities package should go here as another directory
   dir2
      images
      source
          menus
          windows
          // utilities package should go here as another directory
   utilitiesDir
       Classes



From what I understand, you're suggesting that I add a "package utilities" declaration to all the classes in my utilities directory. The problem with that is that the package name would be wrong when I include the utilities directory in the other places; it would have to be source.drawing.utilities in dir1 and source.utilities in dir2.

Are you suggesting something different than that, or am I just not getting it?

Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:20 PM

That does not make sense
RootDir
   dir1
      images
      source
          menus
          drawing
             objects
             // utilities package should go here as another directory
   dir2
      images
      source
          menus
          windows
          // utilities package should go here as another directory
   utilitiesDir
       Classes


The package name of a class can't change on the fly so your utilities are in
rootdir.dir1.source.drawing.objects.xxxx
or in
rootdir.dir2.source.windows.xxxxx
but can't have tyhem in both place the source would be different
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:22 PM

Why do you want to put the source into your source tree? You don't need to put the source for java.lang.String into your source tree, do you?
Was This Post Helpful? 0
  • +
  • -

#10 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:26 PM

How would I include the folder without putting it into the source tree, then? Does that mean I should or shouldn't have my utilities classes in a package? I'm kind of confused here, sorry.

Thanks,
Zel2008

This post has been edited by Zel2008: 05 September 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:34 PM

The source yes or no, why not of it is not confidential code

If you look back to my first reply you can DELETE /S *.java from the temp directory

But again you can't have the same source/class at two different place in a tree

It is exactly for that that packages were invented
Was This Post Helpful? 1
  • +
  • -

#12 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:40 PM

Thanks pbl,

What I meant was, if copying of code isn't necessary, how can I redo my import statements so I can import the classes from the utilities folder into classes in dir1 and dir2? I think I accidentally said "include" before, I've had C++ on the brain for most of the day.

Thanks to both you and Jon for helping me out so far, by the way.

Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Editing and compiling a Java class

Posted 05 September 2012 - 12:41 PM

I'm sorry about the confusion - I'm a little busy at work, and not able to give this my full attention.

1) I think you want to make a package, compile it, and get the compiled code onto your classpath.
2) This is not difficult exactly, but it can be an involved and persnickety process
3) Once the compiled package is on your classpath, it should be visible to your compiler, meaning you should be able to import it into any other project.
4) If you're using an IDE like Eclipse, it's likely that there's some shortcuts built in to facilitate this. Search the help documentation. If not, if you're working in your own file system, like a real programmer, you're going to learn a few things. Make with the google and see what you can find.

Good luck.
Was This Post Helpful? 2
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Editing and compiling a Java class

Posted 05 September 2012 - 01:16 PM

View Postjon.kiparsky, on 05 September 2012 - 03:41 PM, said:

4) If you're using an IDE like Eclipse, it's likely that there's some shortcuts built in to facilitate this.

- Project
-- Properties
--- Build path
---- Librariries
----- Add External .jar

View Postjon.kiparsky, on 05 September 2012 - 03:41 PM, said:

If not, if you're working in your own file system, like a real programmer, you're going to learn a few things.

That will definitively be a good exercise
Was This Post Helpful? 2
  • +
  • -

#15 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Editing and compiling a Java class

Posted 05 September 2012 - 01:42 PM

Wow, this definitely was a good exercise! Lots of fun, I learned a lot. I think I've got everything handled now, thank you pbl and Jon for all your help! I really appreciate you walking me through this.
Thanks again!
Zel2008
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1