Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38230 > unrolled thread

package statement in standalone Java class

Started by"Morten W. Petersen" <morphex@gmail.com>
First post2018-04-07 04:38 -0700
Last post2018-04-07 16:14 -0700
Articles 14 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  package statement in standalone Java class "Morten W. Petersen" <morphex@gmail.com> - 2018-04-07 04:38 -0700
    Re: package statement in standalone Java class Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-04-07 10:25 -0400
      Re: package statement in standalone Java class "Morten W. Petersen" <morphex@gmail.com> - 2018-04-07 08:25 -0700
        Re: package statement in standalone Java class Martin Gregorie <martin@mydomain.invalid> - 2018-04-07 18:23 +0000
          Re: package statement in standalone Java class Arne Vajhøj <arne@vajhoej.dk> - 2018-04-07 15:58 -0400
            Re: package statement in standalone Java class Martin Gregorie <martin@mydomain.invalid> - 2018-04-07 20:28 +0000
              Re: package statement in standalone Java class Arne Vajhøj <arne@vajhoej.dk> - 2018-04-07 16:32 -0400
                Re: package statement in standalone Java class Martin Gregorie <martin@mydomain.invalid> - 2018-04-07 21:35 +0000
                Re: package statement in standalone Java class Eric Douglas <e.d.programmer@gmail.com> - 2018-04-09 04:57 -0700
                  Re: package statement in standalone Java class Arne Vajhøj <arne@vajhoej.dk> - 2018-04-09 18:01 -0400
                    Re: package statement in standalone Java class Arne Vajhøj <arne@vajhoej.dk> - 2018-04-09 18:07 -0400
                      Re: package statement in standalone Java class Arne Vajhøj <arne@vajhoej.dk> - 2018-04-09 19:34 -0400
                      Re: package statement in standalone Java class "Morten W. Petersen" <morphex@gmail.com> - 2018-04-10 16:28 -0700
          Re: package statement in standalone Java class "Morten W. Petersen" <morphex@gmail.com> - 2018-04-07 16:14 -0700

#38230 — package statement in standalone Java class

From"Morten W. Petersen" <morphex@gmail.com>
Date2018-04-07 04:38 -0700
Subjectpackage statement in standalone Java class
Message-ID<d25b03ed-607f-45d0-8b45-25f3835d44bb@googlegroups.com>
Hi there.

I've got a project to learn Java and I've blogged about it here:

http://blogologue.com/search?category=1522415949X05

Now I like to get started with a part like URL finding, using just a single class
file and running it with for example "java DetectURL", but then Maven complains
on "mvn package", because "package org.morphex.app" is missing at the top of the
file, so that "javac DetectURL.java" can work.

Are there for example build-time conditionals I can use, so that the package
statement is present on "mvn package" but not when I javac just that one file?

https://github.com/morphex/twitter-exporter/blob/master/src/main/java/org/morphex/app/DetectURL.java

Thanks & regards,

Morten

[toc] | [next] | [standalone]


#38231

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2018-04-07 10:25 -0400
Message-ID<paakd4$scj$1@dont-email.me>
In reply to#38230
On 4/7/2018 7:38 AM, Morten W. Petersen wrote:
> Hi there.
> 
> I've got a project to learn Java and I've blogged about it here:
> 
> http://blogologue.com/search?category=1522415949X05
> 
> Now I like to get started with a part like URL finding, using just a single class
> file and running it with for example "java DetectURL", but then Maven complains
> on "mvn package", because "package org.morphex.app" is missing at the top of the
> file, so that "javac DetectURL.java" can work.
> 
> Are there for example build-time conditionals I can use, so that the package
> statement is present on "mvn package" but not when I javac just that one file?

     Put `package org.morphex.app;' in the Java source file, put
the source in /...wherever.../org/morphex/app/DetectURL.java, and
simply abandon the "default package."  In the long run -- even in
the short sprint -- you will be much, much better off with your
Java classes arranged in packages.

-- 
esosman@comcast-dot-net.invalid
One thousand nineteen days to go.

[toc] | [prev] | [next] | [standalone]


#38232

From"Morten W. Petersen" <morphex@gmail.com>
Date2018-04-07 08:25 -0700
Message-ID<57ad7674-0fca-4169-81a8-e98b5b084567@googlegroups.com>
In reply to#38231
Hi Eric.

Ok, yes I can understand keeping things organized is a good thing.

But I guess I'd add a Makefile section that strips the first line,
renames files etc. and compiles to enable "unit development" anyway.

I'm new to Java, and having Java, Maven and different libraries to
consider if I get an error seems too complex to debug.

🙂 

-Morten

[toc] | [prev] | [next] | [standalone]


#38233

FromMartin Gregorie <martin@mydomain.invalid>
Date2018-04-07 18:23 +0000
Message-ID<pab2a5$2qe$1@news.albasani.net>
In reply to#38232
On Sat, 07 Apr 2018 08:25:47 -0700, Morten W. Petersen wrote:

> I'm new to Java, and having Java, Maven and different libraries to
> consider if I get an error seems too complex to debug.
> 
Some points to consider:

To a first approximation you should treat Java's 'import' statements as 
the equivalent to C's #include <header.h> preprocessor statements. Just 
as as you can't compile a C program that does anything much without using 
#includes and linking it with the relevant libraries, the only Java 
program you can compile without imports is one that only uses external  
classes from the java.lang group. The big difference it that you never 
need to mention imported classes and packages anywhere else.

I use 'ant' rather than 'maven' - it may be closer to the way make works 
than maven is. The main differences from make are that build.xml is an 
XML document rather than the plaintext with strange ways of using TABs 
and spaces and you find in Makefile and there are built-in XML 
definitions for almost every tool you'll need to build a Java program and 
generate documentation for it.

Don't even think of using 'make' to compile Java. Been there, done that, 
discovered it caiuses problems. While 'make' can be used to build very 
simple programs, the recursive nature of a multi-class Java compile soon 
ties 'make' in knots while 'ant' just gets it done. 

The package names give context to class names, so the custom has grown up 
of using your domain name as the most significant part of a package name 
to guarantee uniqueness, e.g. "com.example.mypackage.MyClass". This 
pretty much guarantees that, although every man and his dog might write 
their own MyClass class and publish it, if you also use this convention 
you'll never end up referencing his MyClass rather than your MyClass. The 
compiler expects this type of structured naming to be reflected in the 
sources its compiling, which is why the source files get names like 
"com/example/mypackage/MyClass.java". So you'd be well-advised to get 
used to working this way as soon as you start writing Java programs that 
contain more than one class.


-- 
Martin    | martin at
Gregorie  | gregorie dot org

[toc] | [prev] | [next] | [standalone]


#38234

FromArne Vajhøj <arne@vajhoej.dk>
Date2018-04-07 15:58 -0400
Message-ID<pab7th$l47$1@gioia.aioe.org>
In reply to#38233
On 4/7/2018 2:23 PM, Martin Gregorie wrote:
> On Sat, 07 Apr 2018 08:25:47 -0700, Morten W. Petersen wrote:
>> I'm new to Java, and having Java, Maven and different libraries to
>> consider if I get an error seems too complex to debug.

> To a first approximation you should treat Java's 'import' statements as
> the equivalent to C's #include <header.h> preprocessor statements. Just
> as as you can't compile a C program that does anything much without using
> #includes and linking it with the relevant libraries, the only Java
> program you can compile without imports is one that only uses external
> classes from the java.lang group.

Strictly speaking you can use any external class without imports - you
just need to use the full name to refer to it.

It looks awfully but it works.

Arne

[toc] | [prev] | [next] | [standalone]


#38235

FromMartin Gregorie <martin@mydomain.invalid>
Date2018-04-07 20:28 +0000
Message-ID<pab9lg$f8j$1@news.albasani.net>
In reply to#38234
On Sat, 07 Apr 2018 15:58:39 -0400, Arne Vajhøj wrote:

> On 4/7/2018 2:23 PM, Martin Gregorie wrote:
>> On Sat, 07 Apr 2018 08:25:47 -0700, Morten W. Petersen wrote:
>>> I'm new to Java, and having Java, Maven and different libraries to
>>> consider if I get an error seems too complex to debug.
> 
>> To a first approximation you should treat Java's 'import' statements as
>> the equivalent to C's #include <header.h> preprocessor statements. Just
>> as as you can't compile a C program that does anything much without
>> using #includes and linking it with the relevant libraries, the only
>> Java program you can compile without imports is one that only uses
>> external classes from the java.lang group.
> 
> Strictly speaking you can use any external class without imports - you
> just need to use the full name to refer to it.
> 
> It looks awfully but it works.
>
I never do that because, as you say, it looks awful. 
Consequently,  I tend to forget its possible.
 

-- 
Martin    | martin at
Gregorie  | gregorie dot org

[toc] | [prev] | [next] | [standalone]


#38236

FromArne Vajhøj <arne@vajhoej.dk>
Date2018-04-07 16:32 -0400
Message-ID<pab9tp$on1$1@gioia.aioe.org>
In reply to#38235
On 4/7/2018 4:28 PM, Martin Gregorie wrote:
> On Sat, 07 Apr 2018 15:58:39 -0400, Arne Vajhøj wrote:
>> On 4/7/2018 2:23 PM, Martin Gregorie wrote:
>>> On Sat, 07 Apr 2018 08:25:47 -0700, Morten W. Petersen wrote:
>>>> I'm new to Java, and having Java, Maven and different libraries to
>>>> consider if I get an error seems too complex to debug.
>>
>>> To a first approximation you should treat Java's 'import' statements as
>>> the equivalent to C's #include <header.h> preprocessor statements. Just
>>> as as you can't compile a C program that does anything much without
>>> using #includes and linking it with the relevant libraries, the only
>>> Java program you can compile without imports is one that only uses
>>> external classes from the java.lang group.
>>
>> Strictly speaking you can use any external class without imports - you
>> just need to use the full name to refer to it.
>>
>> It looks awfully but it works.
>>
> I never do that because, as you say, it looks awful.
> Consequently,  I tend to forget its possible.

I also never do it.

Maybe nobody besides Stefan does it.

Arne

[toc] | [prev] | [next] | [standalone]


#38237

FromMartin Gregorie <martin@mydomain.invalid>
Date2018-04-07 21:35 +0000
Message-ID<pabdif$6j8$1@news.albasani.net>
In reply to#38236
On Sat, 07 Apr 2018 16:32:55 -0400, Arne Vajhøj wrote:

> On 4/7/2018 4:28 PM, Martin Gregorie wrote:
>> On Sat, 07 Apr 2018 15:58:39 -0400, Arne Vajhøj wrote:
>>> On 4/7/2018 2:23 PM, Martin Gregorie wrote:
>>>> On Sat, 07 Apr 2018 08:25:47 -0700, Morten W. Petersen wrote:
>>>>> I'm new to Java, and having Java, Maven and different libraries to
>>>>> consider if I get an error seems too complex to debug.
>>>
>>>> To a first approximation you should treat Java's 'import' statements
>>>> as the equivalent to C's #include <header.h> preprocessor statements.
>>>> Just as as you can't compile a C program that does anything much
>>>> without using #includes and linking it with the relevant libraries,
>>>> the only Java program you can compile without imports is one that
>>>> only uses external classes from the java.lang group.
>>>
>>> Strictly speaking you can use any external class without imports - you
>>> just need to use the full name to refer to it.
>>>
>>> It looks awfully but it works.
>>>
>> I never do that because, as you say, it looks awful. Consequently,  I
>> tend to forget its possible.
> 
> I also never do it.
> 
> Maybe nobody besides Stefan does it.
> 
> Arne

:-)



-- 
Martin    | martin at
Gregorie  | gregorie dot org

[toc] | [prev] | [next] | [standalone]


#38242

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-04-09 04:57 -0700
Message-ID<2774bf88-3719-4fe1-bfb5-190ea9a5f168@googlegroups.com>
In reply to#38236
On Saturday, April 7, 2018 at 4:33:08 PM UTC-4, Arne Vajhøj wrote:
> >> Strictly speaking you can use any external class without imports - you
> >> just need to use the full name to refer to it.
> >>
> >> It looks awfully but it works.
> >>
> > I never do that because, as you say, it looks awful.
> > Consequently,  I tend to forget its possible.
> 
> I also never do it.
> 
> Maybe nobody besides Stefan does it.
> 
> Arne

I do that where necessary of course.  I have had a case or two where I had to reference the same external class name from multiple packages in one of my classes.

[toc] | [prev] | [next] | [standalone]


#38243

FromArne Vajhøj <arne@vajhoej.dk>
Date2018-04-09 18:01 -0400
Message-ID<pagnr6$1s6q$1@gioia.aioe.org>
In reply to#38242
On 4/9/2018 7:57 AM, Eric Douglas wrote:
> On Saturday, April 7, 2018 at 4:33:08 PM UTC-4, Arne Vajhøj wrote:
>>>> Strictly speaking you can use any external class without imports - you
>>>> just need to use the full name to refer to it.
>>>>
>>>> It looks awfully but it works.
>>>>
>>> I never do that because, as you say, it looks awful.
>>> Consequently,  I tend to forget its possible.
>>
>> I also never do it.
>>
>> Maybe nobody besides Stefan does it.
> 
> I do that where necessary of course.  I have had a case or two where
> I had to reference the same external class name from multiple
> packages in one of my classes.

It can be used to work around name conflicts.

But if avoiding wildcard imports, then that should be a relative
rare occasion.

Anyway the above sentiments are missing an UNLESS clause.

Arne



[toc] | [prev] | [next] | [standalone]


#38244

FromArne Vajhøj <arne@vajhoej.dk>
Date2018-04-09 18:07 -0400
Message-ID<pago6l$1t84$1@gioia.aioe.org>
In reply to#38243
On 4/9/2018 6:01 PM, Arne Vajhøj wrote:
> On 4/9/2018 7:57 AM, Eric Douglas wrote:
>> On Saturday, April 7, 2018 at 4:33:08 PM UTC-4, Arne Vajhøj wrote:
>>>>> Strictly speaking you can use any external class without imports - you
>>>>> just need to use the full name to refer to it.
>>>>>
>>>>> It looks awfully but it works.
>>>>>
>>>> I never do that because, as you say, it looks awful.
>>>> Consequently,  I tend to forget its possible.
>>>
>>> I also never do it.
>>>
>>> Maybe nobody besides Stefan does it.
>>
>> I do that where necessary of course.  I have had a case or two where
>> I had to reference the same external class name from multiple
>> packages in one of my classes.
> 
> It can be used to work around name conflicts.
> 
> But if avoiding wildcard imports, then that should be a relative
> rare occasion.
> 
> Anyway the above sentiments are missing an UNLESS clause.

BTW C# has a different rather cute way of handling that.

Example:

using System;

namespace A
{
     public class C
     {
         public override string ToString() { return "I am a A.C"; }
     }
}

namespace B
{
     public class C
     {
         public override string ToString() { return "I am a B.C"; }
     }
}

namespace Apr1
{
     using AC = A.C;
     using BC = B.C;
     public class Program
     {
         public static void Main(string[] args)
         {
             Console.WriteLine(new AC());
             Console.WriteLine(new BC());
         }
     }
}

Arne

[toc] | [prev] | [next] | [standalone]


#38245

FromArne Vajhøj <arne@vajhoej.dk>
Date2018-04-09 19:34 -0400
Message-ID<pagtak$4gt$1@gioia.aioe.org>
In reply to#38244
On 4/9/2018 6:07 PM, Arne Vajhøj wrote:
> BTW C# has a different rather cute way of handling that.

Or a more Java'sk example:

using System;

using UtilDate = java.util.Date;
using SqlDate = java.sql.Date;

namespace E
{
     public class Program
     {
         public static void Main(string[] args)
         {
             Console.WriteLine(new UtilDate(0));
             Console.WriteLine(new SqlDate(0));
             Console.ReadKey();
         }
     }
}

Arne

[toc] | [prev] | [next] | [standalone]


#38246

From"Morten W. Petersen" <morphex@gmail.com>
Date2018-04-10 16:28 -0700
Message-ID<379128cc-26c8-490f-94af-8fbbae88e8df@googlegroups.com>
In reply to#38244
Ick, C#.

🙂 

-Morten

[toc] | [prev] | [next] | [standalone]


#38238

From"Morten W. Petersen" <morphex@gmail.com>
Date2018-04-07 16:14 -0700
Message-ID<448af1c7-9730-43a3-a7e8-a2fa234aa7ae@googlegroups.com>
In reply to#38233
Hi Martin.

Well the point isn't to replace maven or ant, but rather have a way
of bootstrapping and "unit developing".

Maybe I'm overthinking this, and should instead take the time to
fully understand the Maven system when it comes to errors related
to "mvn package".

On the other hand, I could create a Java class that can be called
from the command line and compiles a version without the package
line and downloads any jar dependencies. 🤔

-Morten

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web