java


Setting classpath for a java project with many libraries and external resources in command line


I have the following project. It has some property files in the conf folder, some data in the data folder, some jar files in the lib folder and also some external libraries that are not shown in the photo due to size limitation. Imagine I want to run the RecDriver class. How exactly should I set the classpath so that I can run it in command line? This is how I did it but it does not work as it cannot fine some other files in the project.
C:\Users\myUserName\Downloads\librec-2.0.0\librec-2.0.0\core\src\main\java\net\librec\tool\driver> javac RecDriver.java
The project can be downloaded here:
https://github.com/guoguibing/librec
You can use bin/librec or bin/librec.cmd to run it from commandline.
If you want to build your launch command you can see those start scripts and adapt them for your purposes.
To run your app through command line, once you have the .class files in some dir (usually build) all you have to do is run your application with java -cp "path where jvm can find every .class that you project needs" MainClass.
The -cp flag only tells where to look for compiled .class files, since you are using IntellIJ you can see the command it runs when executing your program, there is a class path that it uses.
Class Path points to where your .class files are, they can be in separate folders, but you need to include every dir when giving the class path, separated by ";"
Example taken from another question in SO.
java -cp "Test.jar;lib/*" my.package.MainClass
Three things to do:
Use the Maven Shade Plugin to create a fat jar (jar with dependencies)
Use the Maven-Jar-Plugin to make the Jar executable
Set <project><build><finalName> to ${artifactId}
Now, after your build ran successfully, you can run your app with
java -jar target/YourArtifactId.jar
(Substitute your project's artifactId for "YourArtifactId")
Okay, here's the full setup.
Add a build section like this to your pom.xml (merge it with any existing one).
<build>
<plugins>
<!-- number 1 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
<!-- number 2 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<!-- number 3 -->
<finalName>${project.artifactId}</finalName>
</build>

Related Links

Euler 14 - Runtime too long (Not really duplicate, I didn't understand how to solve it from similar answer)
Dynamically outputting array information after parsing information
Java compare 2 lists, see if one contains any item from the other
JAVA KeyListnener doesnt reack in snake game (JPanel)
How to convert text into the fields of an object
Some phones need permission READ_CONTACTS to read from contact picker
How to scale control and layout it properly in javafx
cache nor recycle help bitmap loading out-of-memory exception
Method parameters not used in its implementation [closed]
Threads not synchronising propelry
Java - Extending Unknown Classes [duplicate]
No Persistence provider for EntityManager named defaultPersistenceUnit
No matches found for the given name
Netty handlers for different URLs
JFrame not responding to keylistener
Not able to track and get values of MAPI properties of a message .msg file using Aapche_poi_3.10 API

Categories

HOME
sendgrid
tizen
youtube-dl
installshield
user-input
modx-revolution
collectd
moonmail
fortumo
windows-server-2012
evopdf
code-review
django-cms
emulator
java-3d
riot.js
blazemeter
numerical-methods
pepper
kannel
opentype
socialengine
media-queries
bcd
oracle-fusion-middleware
contact-form
siesta-swift
winrt-xaml-toolkit
mpmediaquery
espeak
libraries
sqlite2
leading-zero
nxlog
sas-jmp
cubic-spline
auto-update
youcompleteme
errordocument
tropo
mako
parallel-data-warehouse
removechild
azure-sdk
probability-density
login-control
paypal-express
player
angstrom-linux
srand
separator
reactive-banana
qtableview
t4mvc
rgeo
dukescript
composite
remobjects
typekit
ng-animate
codeigniter-url
gadt
asp.net-web-api-odata
rdl
php-5.4
balanced-payments
coverflow
hungarian-algorithm
jquery-mobile-dialog
smtp-auth
jelly
angularjs-controller
simba
google-email-migration
dice
objective-c-2.0
appconkit
windows-phone-7.1.1
spring-portlet-mvc
mercurial-server
invite
visitor-statistic
asp.net-mvc-areas
dmx512
xsdobjectgen
perfect-hash
fixed-width
firefox4
swing-app-framework
yagni
zend-decorators
putchar
ubuntu-9.04
grid-system
dbisam

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App