Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Runs in Eclipse but not from command line (http://www.velocityreviews.com/forums/t620267-runs-in-eclipse-but-not-from-command-line.html)

Tack 06-14-2008 11:02 PM

Runs in Eclipse but not from command line
 
I have built an application in Eclipse which goes through some JARs,
retrieves the classes within them and introspects the classes to get more
detailed info from them.

I was getting NoClassDefFoundError issues when I tried to introspect some of
the classes (at runtime obviously), which were resolved by adding the
dependency JARs to the Eclipse build path for the project. Now the build
path is configured the program is running fine in Eclipse.

However, when I try to run the program from the command line I am getting
the NoClassDefFoundError errors occurring again. It's pretty clear that the
issues are classpath related. So I've tried the following commands (where
jar1 and jar2 were added to the Eclipse build path):

java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar; -jar myApp.jar

and

set CLASSPATH=c:\jars\jar1.jar;c:\jars\jar2.jar
java -jar myApp.jar

.... but both fail to stop the NoClassDefFoundError problems. For some reason
adding the JARs to the build path in Eclipse stops the issues, but setting
the classpath via the command line doesn't.

Any ideas?


Roland de Ruiter 06-14-2008 11:50 PM

Re: Runs in Eclipse but not from command line
 
On 15-6-2008 1:02, Tack wrote:
> I have built an application in Eclipse which goes through some JARs,
> retrieves the classes within them and introspects the classes to get
> more detailed info from them.
>
> I was getting NoClassDefFoundError issues when I tried to introspect
> some of the classes (at runtime obviously), which were resolved by
> adding the dependency JARs to the Eclipse build path for the project.
> Now the build path is configured the program is running fine in Eclipse.
>
> However, when I try to run the program from the command line I am
> getting the NoClassDefFoundError errors occurring again. It's pretty
> clear that the issues are classpath related. So I've tried the following
> commands (where jar1 and jar2 were added to the Eclipse build path):
>
> java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar; -jar myApp.jar
>
> and
>
> set CLASSPATH=c:\jars\jar1.jar;c:\jars\jar2.jar
> java -jar myApp.jar
>
> ... but both fail to stop the NoClassDefFoundError problems. For some
> reason adding the JARs to the build path in Eclipse stops the issues,
> but setting the classpath via the command line doesn't.
>
> Any ideas?

When you use the -jar option, the JAR file is the source of all user
classes, and other user class path settings are ignored. Though you can
set a class path attribute in the manifest file.
<http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html#-jar>
<http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html#Main%20Attributes>
--
Regards,

Roland

Andrew Thompson 06-15-2008 04:11 AM

Re: Runs in Eclipse but not from command line
 
On Jun 15, 9:50*am, Roland de Ruiter
<roland.de.rui...@example.invalid> wrote:
> On 15-6-2008 1:02, Tack wrote:
>
> > I have built an application in Eclipse which goes through some JARs,
> > retrieves the classes within them and introspects the classes to get
> > more detailed info from them.


Why? What does this do for the end user?
...
> > java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar; -jar myApp.jar

...
> When you use the -jar option, the JAR file is the source of all user
> classes, and other user class path settings are ignored. Though you can
> set a class path attribute in the manifest file.


Another way is to drop the jar option, add the
main jar to the classpath, and call the main class
explicitly.

java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar;.\myApp.jar
com.our.Main

--
Andrew T.
http://pscode.org/

Andrew Thompson 06-15-2008 05:04 AM

Re: Runs in Eclipse but not from command line
 
On Jun 15, 2:43*pm, Lew <con...@lewscanon.com.invalid> wrote:
> Andrew Thompson wrote:
> > On Jun 15, 9:50 am, Roland de Ruiter
> > <roland.de.rui...@example.invalid> wrote:
> >> On 15-6-2008 1:02, Tack wrote:

>
> >>> I have built an application in Eclipse which goes through some JARs,
> >>> retrieves the classes within them and introspects the classes to get
> >>> more detailed info from them.

> > ...
> >>> java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar; -jar myApp.jar

> > ...
> >> When you use the -jar option, the JAR file is the source of all user
> >> classes, and other user class path settings are ignored.

...
> > Another way is to drop the jar option, add the
> > main jar to the classpath, and call the main class
> > explicitly.

...
> True, but "java -jar" is preferable - it treats the JAR as a deployable
> application container, and describer of its own dependencies.
>
> Java's insistence that "java -jar" ignore the classpath is, uhh, jarring when
> you first encounter it, but there's good reason for it. *Most of us aren't
> used to thinking as deployers, only programmers.

(snip reasoning..)

Uh-huh (the penny drops). Finally that irritating
behaviour of the jar option makes some sense*.

It reminds me of many other deployment issues
particularly related to both the application
classpath and (slightly on a tangent) the security
environment that I always wished that we could
'tighten down' during development to get a better
idea of the challenges of real world deployment.

* Though it would be more ideal to be able to
select the behavior explicitly, as a param to
the java command, IMO.

--
Andrew T.
http://pscode.org/

Tack 06-15-2008 10:57 AM

Re: Runs in Eclipse but not from command line
 

"Andrew Thompson" <andrewthommo@gmail.com> wrote in message
news:329acd29-780b-446c-ae3f-ad46a69c567d@n19g2000prg.googlegroups.com...
On Jun 15, 9:50 am, Roland de Ruiter
<roland.de.rui...@example.invalid> wrote:
> On 15-6-2008 1:02, Tack wrote:
>
> > I have built an application in Eclipse which goes through some JARs,
> > retrieves the classes within them and introspects the classes to get
> > more detailed info from them.


> Why? What does this do for the end user?


.... because the end-user wants an XML representation of the classes.
Although this is besides the point.

> > java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar; -jar myApp.jar

....
> When you use the -jar option, the JAR file is the source of all user
> classes, and other user class path settings are ignored. Though you can
> set a class path attribute in the manifest file.
>
>Another way is to drop the jar option, add the
>main jar to the classpath, and call the main class
>explicitly.
>
>java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar;.\myApp.jar
>com.our.Main


This is what I ended up doing. Thanks.


Andrew Thompson 06-15-2008 12:07 PM

Re: Runs in Eclipse but not from command line
 
On Jun 15, 8:57*pm, "Tack" <t...@hotmail.com> wrote:
> "Andrew Thompson" <andrewtho...@gmail.com> wrote in message

...
> > ...What does this do for the end user?

>
> ... because the end-user wants an XML representation of the classes.
> Although this is besides the point.


The point of the strategy perhaps, but not
the point of the end goal.

Is this a development tool?

> >java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar;.\myApp.jar
> >com.our.Main

>
> This is what I ended up doing. ...


I am surprised, given the discussion that followed.

How will you deploy the application?

(There are deployment techniques that make
a Manifest redundant, but for a 'plain old
non-webstart app', it is very useful.)

--
Andrew Thompson
http://pscode.org/

Tack 06-15-2008 01:59 PM

Re: Runs in Eclipse but not from command line
 

"Andrew Thompson" <andrewthommo@gmail.com> wrote in message
news:12a73450-05e2-498d-a29a-d3e220e99ed1@s33g2000pri.googlegroups.com...
On Jun 15, 8:57 pm, "Tack" <t...@hotmail.com> wrote:
>> "Andrew Thompson" <andrewtho...@gmail.com> wrote in message

>...
>> > ...What does this do for the end user?

>>
>> ... because the end-user wants an XML representation of the classes.
>> Although this is besides the point.

>
>The point of the strategy perhaps, but not
>the point of the end goal.
>
>Is this a development tool?


Yes, it's a utility as part of a larger development tool.

>> >java -classpath c:\jars\jar1.jar;c:\jars\jar2.jar;.\myApp.jar
>> >com.our.Main

>>
>> This is what I ended up doing. ...

>
>I am surprised, given the discussion that followed.


Well, I actually did a hybrid of two suggestions. I updated the manifest
with

Class-Path: .

.... and put myApp.jar into the same folder as the dependencies, navigated to
the folder and used the following code to run the app:

java -classpath jar1.jar;jar2.jar;myApp.jar com.our.Main

>How will you deploy the application?


It will be configured for my development environment, which is also shared
as part of an Subversion repository.

I cannot specify the dependency JARs within the manifest because they can
change at run-time. The application will use the latest JARs build by other
developers. These are versioned with different names, so I'm trying to make
them configurable in the .bat file used to run the app.


Tom Anderson 06-15-2008 04:14 PM

Re: Runs in Eclipse but not from command line
 
On Sun, 15 Jun 2008, Lew wrote:

> Tack wrote:
>> Well, I actually did a hybrid of two suggestions. I updated the manifest
>> with
>>
>> Class-Path: .

>
> No one suggested that.
>
> <http://java.sun.com/javase/6/docs/te.../jar.html#Main
> Attributes>
>
>> ... and put myApp.jar into the same folder as the dependencies, navigated
>> to the folder and used the following code to run the app:
>>
>> java -classpath jar1.jar;jar2.jar;myApp.jar com.our.Main

>
> Better would be:
>
> Manifest has
> Main-Class: com.our.Main
> Class-Path: jar1.jar jar2.jar
>
> Deployment has all three JARs in the same directory, call it /usr/foo/.


I think the OP needs to be able to vary the set of depended-on JARs at
runtime - this is essentially a kind of plugin mechanism. That means
specifying them in the app JAR's manifest won't work. Unless i've
misunderstood what you're doing here.

> $ cd /usr/foo
> $ jar -jar myApp.jar


Yousa thinking yousa jars ganna run?

tom

--
That must be one of the best things you can possibly do with a piglet,
booze and a cannon. -- D

Tack 06-15-2008 04:25 PM

Re: Runs in Eclipse but not from command line
 
"Tom Anderson" <twic@urchin.earth.li> wrote in message
news:Pine.LNX.4.64.0806151709500.20984@urchin.eart h.li...
> On Sun, 15 Jun 2008, Lew wrote:
>
>> Better would be:
>>
>> Manifest has
>> Main-Class: com.our.Main
>> Class-Path: jar1.jar jar2.jar
>>
>> Deployment has all three JARs in the same directory, call it /usr/foo/.

>
> I think the OP needs to be able to vary the set of depended-on JARs at
> runtime


This is exactly what I'm trying to do.


Arne Vajh°j 06-15-2008 05:26 PM

Re: Runs in Eclipse but not from command line
 
Andrew Thompson wrote:
> On Jun 15, 2:43 pm, Lew <con...@lewscanon.com.invalid> wrote:
>> True, but "java -jar" is preferable - it treats the JAR as a deployable
>> application container, and describer of its own dependencies.
>>
>> Java's insistence that "java -jar" ignore the classpath is, uhh, jarring when
>> you first encounter it, but there's good reason for it. Most of us aren't
>> used to thinking as deployers, only programmers.

> (snip reasoning..)
>
> Uh-huh (the penny drops). Finally that irritating
> behaviour of the jar option makes some sense*.


I would put it even more directly: end users typical
want just to double click on the jar file. That fits
well with the -jar implementation while explicit use
of -cp would not work for that category of users.

Arne


All times are GMT. The time now is 04:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.