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

Change $JAVA_HOME in python script
Squirrel SQL display icon disappears when Squirrel is running
JVM memory settings impact
My Java System.out.print's seem to overlap slightly
Image is not being displayed with setIcon()
Unable to instantiate activity (Android)
Add a method to an exited ArrayList
Alert user if item already exists within a HashSet
JAVA classes and objects…what will be in my class
Tomcat connection pool & idle connections
JAVA: Open and read file using InputStreamReader
Printing the Index of an array in Java
Using regex to replace the punctuation marks and use minimum length of words
JPA Criteria API - arithmetic with Calendar values
How does jdbc:initialize-database work underneath the lid?
Spring Boot MVC Multi-Module Executeable jar

Categories

HOME
java
compiler-construction
date
wso2
keras
image-processing
azure-data-factory
gremlin
reserved
view
raspberry-pi
vmware
filtering
baqend
android-youtube-api
dacpac
row
collectd
moonmail
msp430
clojurescript
seaborn
ef-migrations
finite-automata
propel
solaris-10
interop
lldb
format-specifiers
claims-based-identity
facebook-access-token
virtualdub
rst2pdf
spring-mybatis
typo3-6.2.x
elasticsearch-net
vision
bpel
scorm2004
code-contracts
jmonkeyengine
espeak
webix-treetable
ssjs
dynamic-reports
angular2-meteor
scrollable
parentheses
zip4j
dds
pnotify
aurelia-validation
tactic
nodebb
np-complete
windows-mobile-6.5
gcsfuse
background-service
topbeat
nxt
sts-springsourcetoolsuite
gridview-sorting
flash-cs5
react-native-listview
lua-5.1
ubuntu-10.04
rvest
python-3.2
embedded-code
operation
streambase
python-green
dd
project-planning
funcunit
wp-query
expected-exception
hippomocks
comexception
gnu-smalltalk
multiprocessor
inbox
cos
jelly
wsdl-2.0
distutils
hamiltonian-cycle
bitsharp
broken-links
ocx
appconkit
jquery-ui-layout
netdna-api
xmlspy
yetanotherforum
imac
gamma
iweb
ctp4
simpletest
zune
msdev
geneva-server
grid-system
misv

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