Error in Java program missing packages - java

I tried to run the java program but missing packages.
import org.activiti.engine.*;
public class RawDriver
{
public static void main(String[] args) {
ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration().setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE).setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000").setJobExecutorActivate(true).buildProcessEngine();
System.out.println("processEngine = " + processEngine);
}
}
How can i import external packages and run the program?

The external packages need to be in your classpath. This can be set as an environment variable or on the command line. You need to have the libraries associated with what you need which are usually shipped as jar files. More about the classpaths here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

if the external packages are in another project, you can import that project, then in the project you are writing, choose Java build path -> project, add the project you just import. Then go to your java file and import the packages.

If you are using esclipse , if you have a jar file then you can add the jar file to you classpath using the project --->Properties-->java build path
add the jar in the project libraries , you will be able to access external packages like this .
if you using command line option or unix , set the class path using the command line option set CLASSPATH option.
Hope this helps
Abhi

Related

NoClassDefFoundError when runnable jar

Hi Guys I have included the Webcam-Capture API in my project.
When I run it in Netbeans everything works fine. But when i compile everything to a runnable jar i get this message trying to run it by cmd line.
can anyone of you help me?
i already tried to unbound and rebound all jars and changing jdks but its not working
add -classpath flag in the command line ,pointing to the path where Webcam-Capture API exists in your file system, unless you want to create a single package executable.In your case It should be something like below
java -classpath YOURJAR.jar;folder_of_dependant_jar/*;. com.awesome.pagackage.Starter
Where YOURJAR.jar contains the com.awesome.pagackage.Starter.main(String args[])
You also mentioned that your jar is a runnable jar it also means that while exporting/building you can do one of the following way.( NOTE , this feature is in eclipse , but you would get the idea ).Each of the following options you see in the library handling does specific things.
The first option: Extracts the dependent jar into your target jar as java packaging.This means if your package is com.awesome.package and the dependent jar has package logic.package; , after the runnable jar is build you could find both these package exists in your jar file.
The second option: I think it is more on eclipse specific since eclipse adds few classes of its own , of runnable generation, so I am not explaining it here.
The third option : is the most interesting one. it creates folder stucture like below
ndon_lib\external.jar ( external jar file )
ndon.jar ( your jar file )
This time the manifest.mf file contains something like below.
Class-Path: . ndon_lib/external.jar
Main-Class: com.awesome.pagackage.Starter
You should set the classpath
java -cp "your.jar" "yourclass"

What does "Exception in thread \"main\" java.lang.NoClassDefFoundError" mean when executing java .class file?

Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.

Why does my program not find my java classes?

I have a project that I have compiled to a jar with Maven which I named H.jar.
The maven command I use is (in eclipse):
maven install
The jar file has a class called Person in it.
I have added the jar file to the classpath on a Windows machine.
echo %classpath%
Results -> C:\location_to_jar\H.jar
But when I try to compile the program I get error:
error. Cannot find symbol.
I am running the command:
javac Main.java
The class looks like this:
public class Main {
public static void main(String[] args) {
Person p = new Person("John", "Doe");
p.toString();
}
}
Should the program not just find the class if I have added it to the classpath?
It sounds like your program is fully contained within a single jar, with no external dependencies, so I would recommend that you avoid using the %CLASSPATH% environment variable. Instead, point Java at the jar directly.
Assuming that your Maven build has generated an executable jar with your Main class defined as its entry point then you can just execute:
java -jar C:\location_to_jar\H.jar
If the executable class has not been defined in your build, then you can define the classpath on the command line:
java -cp C:\location_to_jar\H.jar com.mypackage.Main
You will need to replace the package in the command above with the appropriate package name as defined in your application.
If you want Maven to make your jar executable, then you can use the Maven Assembly Plugin to help. An answer which explains that can be found here: How can I create an executable JAR with dependencies using Maven?

Using ClearNLP Semantic Role Labeler

I want to use clearNLP (http://clearnlp.wikispaces.com/) for extracting semantic role labels of an input sentence. I followed the instructions here: http://clearnlp.wikispaces.com/installation (I downloaded the jar files, put them in a directory called ClearNLP and set the classpath) but when I run the command java com.clearnlp.run.Version, I face the error: Could Not find or Load Main.
I tried it twice: Once I set the classpath as an environment variable of windows and ran the command in CMD. But, when it didn't work, I tried to create a java project, set the libraries using NetBeans and run the program. But, it didn't work, too.
BTW, when I run echo %classpath% command, I see that the classpath is set correctly.
Can anybody help me?
Try Eclipse. I included the jars in a new project I created. I then created a simple class like so
package test;
import com.clearnlp.run.Version;
public class TestClearNLP {
public static void main String(args[]) {
Version.main(args);
}
}
When run, this creates output in the console of:
ClearNLP version 2.0.2
Webpage: clearnlp.com
Owner : Jinho D. Choi
Contact: support#clearnlp.com
The only weird situation I ran into was that Eclipse did not like the jar files beginning with a period. I removed those from my project and ran with the remaining libraries.

Java: Adding included Jar files to a created Jar file in linux

I need help including imported jar files into my java program in Linux. Here is the program:
import java.sql.*;
public class CreateCoffees
{
public static void main(String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
System.exit(1);
}
}
}
In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath:
db2jcc_license_cu.jar
db2jcc4.jar
I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this:
javac CreateCoffees.java
java CreateCoffees
But I got this error
ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
Then I tried the "-classpath" option
javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees
but got this
Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees
How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ?
Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly
How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.

Categories

Resources