Help a newbie please?

S

SpreadTooThin

haven't written a line of java in years... but today I have to write
a script. (On a MAC in XCode)

So I found this on the internet:

http://stackoverflow.com/questions/14617/java-what-is-the-best-way-to-sftp-a-file-from-a-server

Nice simple method
I downloaded j2ssh which has a few jar files in them.
Obviously this won't compile / run unless the right jar file is in the
right place.

j2ss contains:

j2ssj-ant-0.2.9.jar
j2ssh-common-0.2.9.jar
j2ssh-core-0.2.9.jar
j2ssh-daemon-0.2.9.jar
and lib:
BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar


So where do I put these files so that my app will compile / run?

TIA
 
O

Owen Jacobson

haven't written a line of java in years... but today I have to write
a script. (On a MAC in XCode)

So I found this on the internet:

http://stackoverflow.com/questions/14617/java-what-is-the-best-way-to-sftp-a-file-from-a-server

Nice
simple method
I downloaded j2ssh which has a few jar files in them.
Obviously this won't compile / run unless the right jar file is in the
right place.

j2ss contains:

j2ssj-ant-0.2.9.jar
j2ssh-common-0.2.9.jar
j2ssh-core-0.2.9.jar
j2ssh-daemon-0.2.9.jar
and lib:
BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar


So where do I put these files so that my app will compile / run?

On your application's classpath:

http://java.sun.com/javase/6/docs/technotes/tools/solaris/classpath.html

Note: do _not_ use the CLASSPATH environment variable; you will likely
break other Java applications. Use the -classpath startup parameter or
the Class-Path manifest attribute, or your IDE's classpath tools. Also,
you probably don't want to include jdk13-118.jar - try without that one
first.

-o
 
S

SpreadTooThin

On your application's classpath:

http://java.sun.com/javase/6/docs/technotes/tools/solaris/classpath.html

Note: do _not_ use the CLASSPATH environment variable; you will likely
break other Java applications. Use the -classpath startup parameter or
the Class-Path manifest attribute, or your IDE's classpath tools. Also,
you probably don't want to include jdk13-118.jar - try without that one
first.

-o

As I am in xcode, and xcode uses a build.xml file. Should I not
modify the build.xml file somehow?
(I think that's like an ant standard)

Also, given the names of the jar files I mentioned above, should my
code import them?
I mean
import j2ssh.*;
or
import j2ssh-common-0.2.9;
import j2ssh-daemon-0.2.9;
import j2ssh-ant-0.2.9;
import j2ssh-core-0.2.9;
 
R

Roedy Green

Obviously this won't compile / run unless the right jar file is in the
right place.

see http://mindprod.com/jgloss/ext.html
http://mindprod.com/jgloss/classpath.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"At this point, 29 percent of fish and seafood species have collapsed - that is,
their catch has declined by 90 percent. It is a very clear trend, and it is accelerating.
If the long-term trend continues, all fish and seafood species are projected to collapse
within my lifetime -- by 2048."
~ Dr. Boris Worm of Dalhousie University
 
L

Lew

SpreadTooThin said:
As I am in xcode, and xcode uses a build.xml file.  Should I not
modify the build.xml file somehow?

If you read the Ant documentation they document how to specify
classpaths.
(I think that's like an ant standard)

Precisely like.
Also, given the names of the jar [sic] files I mentioned above, should my
code import them?

You import classes, not JARs.
I mean
import j2ssh.*;
or
import j2ssh-common-0.2.9;
import j2ssh-daemon-0.2.9;
import j2ssh-ant-0.2.9;
import j2ssh-core-0.2.9;

No. Those imports are meaningless.

Use 'import' to specify the fully-qualified name (FQN) of a class so
that you can use the simple name in the rest of the code. Check out
the Java tutorial on java.sun.com.

As an example, if you use the type java.util.Collection, you can
either refer to it by the FQN in your code:

public class Foo
{
private java.util.Collection stuff;
...
}

or import the name (not the class!):

import java.util.Collection;
public class Foo
{
private Collection stuff;
...
}

The purpose of 'import' is strictly a convenience for source code to
make names shorter and more readable. It has no relevance to bytecode
and doesn't do anything to classes.

Read the tutorial.
 
S

SpreadTooThin

If you read the Ant documentation they document how to specify
classpaths.

So then I modify the jarpath?
Precisely like.
Like totally almost. :)

Also, given the names of the jar [sic] files I mentioned above, should my
code import them?

You import classes, not JARs.
I mean
import j2ssh.*;
or
import j2ssh-common-0.2.9;
import j2ssh-daemon-0.2.9;
import j2ssh-ant-0.2.9;
import j2ssh-core-0.2.9;

No.  Those imports are meaningless.

Use 'import' to specify the fully-qualified name (FQN) of a class so
that you can use the simple name in the rest of the code.  Check out
the Java tutorial on java.sun.com.

As an example, if you use the type java.util.Collection, you can
either refer to it by the FQN in your code:

Right like 'using namespace something' in c++... If I understand you.
public class Foo
{
  private java.util.Collection stuff;
  ...

}

or import the name (not the class!):

import java.util.Collection;
public class Foo
{
  private Collection stuff;
  ...

}

The purpose of 'import' is strictly a convenience for source code to
make names shorter and more readable.  It has no relevance to bytecode
and doesn't do anything to classes.

Read the tutorial.

Ok but I'm still wondering what the FQN is of methods / names in the
jar files are.
and the syntax of the import line. I mean can you tell from the jar
file?
 
M

Mark Space

SpreadTooThin said:
Ok but I'm still wondering what the FQN is of methods / names in the
jar files are.
and the syntax of the import line. I mean can you tell from the jar
file?

Normally that would be in the documentation. If there's source code in
the jar file, you can gen the Javadoc, or you can look at the source
code. If there's no source, you could still generate a list of classes
and methods (with javap, I think), but that's going to be of limited
use. Look for the docs.


Jar files are just zip files, and you can look at their contents with
any zip viewer. The jar command (in the jdk) has the same syntax as the
Unix ar command:

jar -tf jarname

will print the contents of a file "jarname" to stdout. Depending on
your IDE, you might be able to open the jarfiles or add them as
libraries, or add the jar files to a "doc path". These actions might
let you see inside a jar file.
 
S

SpreadTooThin

 haven't written a line of java in years... but today I have to write
a script. (On a MAC in XCode)

So I found this on the internet:

http://stackoverflow.com/questions/14617/java-what-is-the-best-way-to...

Nice simple method
I downloaded j2ssh which has a few jar files in them.
Obviously this won't compile / run unless the right jar file is in the
right place.

j2ss contains:

j2ssj-ant-0.2.9.jar
j2ssh-common-0.2.9.jar
j2ssh-core-0.2.9.jar
j2ssh-daemon-0.2.9.jar
and lib:
BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar

So where do I put these files so that my app will compile / run?

TIA

Ok I've figured out what is going on.. Sort of.. At least I can make
my application now, but Its not running.
I had to copy the jar files to /System/Library/Java/Extensions.
and
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.SftpClient;

However there was a lib sub-directory in the distribution of j2ssh and
I don't know what I was supposed to do with that folder... That
folder contained:

BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar


Now when I run my application:

$>java -jar testsftp.jar

I get an error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/
commons/logging/LogFactory
at com.sshtools.j2ssh.SshClient.<clinit>(Unknown Source)
at testsftp.main(testsftp.java:23)

I suspect that because there was a jar file in the lib folder that
maybe I needed to copy that lib folder as well?
 
J

John B. Matthews

SpreadTooThin said:
As I am in xcode, and xcode uses a build.xml file.

I generally use NetBeans, Eclipse or the command line instead of Xcode
for Java development. I didn't know Xcode could use an ant script. There
are none present in v3.1.2:

$ find /Developer/Examples/Java -name build.xml
Should I not modify the build.xml file somehow?

Well, if you have a build.xml, try ant -p from the command line to see
what targets are available.

If you want to explore Xcode, try the examples mentioned above.
 
A

Arne Vajhøj

SpreadTooThin said:
Ok I've figured out what is going on.. Sort of.. At least I can make
my application now, but Its not running.
I had to copy the jar files to /System/Library/Java/Extensions.

Not good.

Specify them explicit either to java command line or to java ant task.

However there was a lib sub-directory in the distribution of j2ssh and
I don't know what I was supposed to do with that folder... That
folder contained:

BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar

They should also be in classpath.

(the good thing is that Java 1.6 supports wildcards for jar files
in classpath !!)
Now when I run my application:

$>java -jar testsftp.jar

I get an error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/
commons/logging/LogFactory
at com.sshtools.j2ssh.SshClient.<clinit>(Unknown Source)
at testsftp.main(testsftp.java:23)

I suspect that because there was a jar file in the lib folder that
maybe I needed to copy that lib folder as well?

Yes. It can not find commons-logging.jar !

When you use -jar then you should put all the required jar
files in the Class-Path directive in the manifest.

Arne
 
L

Lew

Not good.
Truly.

Specify them explicit either to java [sic] command line or to java [sic] ant task.
However there was a lib sub-directory in the distribution of j2ssh and
I don't know what I was supposed to do with that folder... That
folder contained:

BOUNCYCASTLE.LICENSE
COMMONS.LICENSE
XERCES.LICENSE
commons-logging.jar
jdk13-119.jar
xercesImpl.jar
xmlParserAPIs.jar
They should also be in classpath.

(the good thing is that Java 1.6 supports wildcards for jar files
in classpath !!)

You need that class in your class path.
Yes. It can not find commons-logging.jar !

When you use -jar then you should put all the required jar
files in the Class-Path directive in the manifest.

<http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html>

Your JAR should go in a distribution directory specific to your app, not the
extensions folder, not a system folder, not a different application's folder.
This "lib/" folder of which you speak needs to be a subdirectory of that
application directory, assuming it's referred to as a relative path in the
manifest's Class-Path header and that the other JARs are mentioned there as
Arne advised.

RTFM will help you a lot.
 
O

Owen Jacobson

Ok I've figured out what is going on.. Sort of.. At least I can make
my application now, but Its not running.
I had to copy the jar files to /System/Library/Java/Extensions.

This will also break other Java programs: libraries in
/System/Library/Java/Extensions (or lib/ext on other platforms :) are
visible to every single Java program run by that VM, including programs
that may be expecting a different version of those libraries.

Use the -classpath command-line and Class-Path: manifest tools. Don't
use the extensions dir or a global CLASSPATH environment variable.
Local changes are much easier to deal with than global changes to your
system.

You asked elsewhere in this thread how to find out what classes and
methods are in a librarly. A little googling suggests that the "j2ssh"
JARs are from the SSHTools project, whose website is
<http://sshtools.sourceforge.net/>. Normally, a project's website also
has documentation; sshtools seems to have failed at this as their
website is nearly empty. You can also get a list of files in a JAR:

$ jar tf commons-logging-1.1.1.jar
[...]
org/apache/commons/logging/impl/Jdk14Logger.class
org/apache/commons/logging/impl/Log4JLogger.class
[...]

Each .class file corresponds to a Java class, so this JAR contains
org.apache.commons.logging.impl.Jdk14Logger,
org.apache.commons.logging.impl.Log4JLogger, and so on.

Unfortunately, this tells you nothing about how to use the library;
it's merely a giant list of every class it contains, including ones you
can't (or shouldn't) use directly. Fortunately, SSHTools' Sourceforge
download page has both documentation and tutorials:
<http://sourceforge.net/project/showfiles.php?group_id=60894>

It sounds like you're using Ant to build your project, so you should
probably have a look at the Ant manual pages for the 'javac'
(http://ant.apache.org/manual/CoreTasks/javac.html) and 'java'
(http://ant.apache.org/manual/CoreTasks/java.html) tasks. If you're
planning on using your program outside of Ant in the future, you'll
want to look at the 'jar'
(http://ant.apache.org/manual/CoreTasks/jar.html) task for packaging
your own code. Distribute both your own JAR and the JARs you depend on;
don't re-package your libraries into your JAR.

HTH
-o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top