0

I'm currently using linux and i have to run a command in the form

java -cp .:../jarfile.jar doc.DocDump <file>

suppose currently that i'm in folder /home/noor/downloads/commands/commandDump. Suppose, I need to run the command from anywhere, I'm trying the command below but its not running.

suppose that i'm currently in home

java -cp .:/home/noor/downloads/commands/commandDump/../jarfile.jar doc.DocDump

the "commands" folder contain the jarfile.jar and also contain a folder "command" which then contain a folder "doc" which then contain the class "DocDump"

I'm getting this error:

 Exception in thread "main" java.lang.NoClassDefFoundError: Doc/DocDump
Caused by: java.lang.ClassNotFoundException: Doc.DocDump
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: doc.DocDump. Program will exit.
0

3 Answers 3

1

I would recommend you to look into the manifest file documentation, in particular the classpath section.

Specifying the dependencies in the manifest file, apart allowing a more compact syntax when launching from the command line, can specify entries in directories relative to the main jar and not to the current directory.

Remember that in that case you will have to launch your application with:

java -jar <<yourjar.jar>> <<options>>
Sign up to request clarification or add additional context in comments.

2 Comments

With java -jar you are unable to specify other stuff in your classpath (the -cp option is ignored if -jar is provided, as far as I know).
True - with -jar you can only specify the classpath in the manifest file. With the exception of a few corner cases, this is not a limitation. Also, there are apps (including eclipse) which give you the ability to pack jars inside jars so you can distribute your app as a single file.
0

Linux is case sensitive on the paths. Your path contains the "downloads" directory while in the java command you are using the "Downloads" directory.

Also, the class to be run will be the same: doc.DOCDump everywhere. You specify the class using the package.className and not some path to the class file.

3 Comments

i'm using this command but still some error, java -cp .:/home/noor/downloads/commands/commandDump/../jarfile.jar doc.DocDump
Is the path to your jar file correct? Does the jar file contain a package doc and inside it a class DocDump?
now its like this java -cp .:/home/noor/downloads/commands/jarfile.jar doc.DocDump but same error
0

The class file you provide as main does not need any file path prefix. Just type:

java -cp .:/home/noor/Downloads/commands/jarfile.jar doc.DocDump <file>

The virtual machine will look for that doc.DocDump class within the classpath (which you already specified).

1 Comment

the "commands" folder contain the jarfile.jar and also contain a folder "command" which then contain a folder "doc" which then contain the class "DocDump"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.