So i have a maven project in eclipse that i can normally start via run as> Java Application. If i however try to start the class file from the target directory from the commandline via "java TestServer" it wont work. It says it cant find or load the main class. If i replace the sourcecode with a simple dummy hello world and execute then it works fine.
The code has two maven dependencies.
Simple dummy:
public class HelloWorld {
public static void main(String[] args){
System.out.println("hello world!");
}
}
Why does this happen? Is there a problem due to the nestest class? Why can't a main class be found for the code but for a small dummy?
The problem is the classpath.
Maven is not responsible for running the project from Eclipse workspace
because Eclipse holds its own .project file, which contains all classpath entries. When you use Run As, Eclipse just uses all classpath configurations of its .project file.
To start the java program from console, you need to set the classpath to your bin or target directory and to all libraries ('jar') which are referenced by your project. After setting the classpath correctly you can start your program with java <qualified classname>. Starting the program this way does not use any of Maven's functionallity yet at all.
Have a look here to use Maven https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Maven will create a library jar which contains all classes of your project.
You can set the classpath to the generated library and start your programm using java <qualified classname>
EDIT due to comment
Here is an example for setting the classpath using the Windows OS
console. You can put this line also into an appropriate Windows batch (.bat)
file and start your program,
set classpath = .;log4j.jar;lib/any-other-lib.jar
java org.<whatever>.MyProgram
for further information for setting the classpath on other OS's you may also have a look at
setting Java Classpath in linux?
Related
When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.
I am trying to run a java program that uses protobuf.jar, but I keep getting this error.
I have set my classpath variable in linux so that:
CLASSPATH=/home/.../src/PlaceServer.class:/home/.../src/protobuf.jar:/home/.../src
export CLASSPATH
But then when I run my program in the command line after reading in the jar.
java PServer
I get this:
java.lang.NoClassDefFoundError: com/google/protobuf/MessageOrBuilder
However when I run another program that also reads the same jar, this one called BServer
java BServer
It works fine, and correctly as I want it. I even tried running under these commands instead
java -cp .protobuf.jar PServer
And it still did not work for PServer.
However, if I run the same programs on my Macbook from the command line (also in Eclipse in either OS) I do not get this Exception and it all works fine.
Thanks for your help!
There are a couple of things to check to get rid of this error:
Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.
The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution
When you run the application in Eclipse, the IDE resolves this by using the .classpath file inside the project folder. When you build an application (create the jar), you could accidentally omit this class, or change its location.
What you need to do is to first open the jar, and make sure that the class in question is in fact inside the jar, in the same path. Then, go through the list above.
I'm trying to run a small Play application (well, the tests really) in Eclipse and I've come up against a frustrating problem. When I run the command from the command line all the tests are successful, but from Eclipse I've found that it is unable to load some of our required properties files. Looking at the target directory I see two compiled class folders, classes and classes_managed. The properties files are in the classes directory, as I'd expect, but when I look to the class that's attempting to load the properties file the classpath appears to only have the classes_managed directory, which doesn't contain the properties files that are required.
For reference I am running these as JUnit tests so that I can debug them. I've run play eclipse on the command line and imported the project to Eclipse as suggested in the documentation. I'm using Eclipse Kepler and Play 2.2.3.
Any help would be appreciated.
If those files are not getting added to the classpath properly, you can ensure they're accessible by specifically telling sbt that they need to be on the classpath with:
unmanagedClasspath in Test <+= baseDirectory map { bd => Attributed.blank(bd / "conf") }
put this line somewhere in your build.sbt file
I'm pretty new to programming, so this should be an easy one. I'm switching from eclipse to netbeans and I am trying to add libjinput-osx.jnilib to my working directory. I have step by step instructions for eclipse, but not netbeans. I'm about 2.5 hours and 65 google searches in and I still cant find the answer to these two basic questions I have.
What exactly is a working directory in java?
How do you add a .jnilib file to your working directory in netbeans?
My end goal is to get an xbox controller to control a game of snake I wrote. I am trying to use JInput and this tutorial. In order to compile properly on OSX I need to put libjinput-osx.jnilib in the "working directory".
The "working directory" is the location where the application is working in...cryptic huh ;)
Typically it's the launch location for the app, although this can be modified through flags and other process.
In Netbeans, the working directory is typically the root directory of the project (which contains the src and nbproject folder), but this can be changed via the project properties.
One of the simplest ways to find the working directory at run time (this is useful for testing) is to do some thing like...
System.out.println(new File(".").getAbsolutePath());
There are two aspects to this question.
When you run a command from the command line, the "working directory" is the directory you were in when you ran the command. The instructions that you are reading say to put the native library there because Java's default search path for native library includes the current directory. Alternatively, you could add a -Djava.library.path=... option to the java command to set the search path explicitly.
When you run a command from within Eclipse ... or NetBeans ... the IDE's launcher will set the appropriate JVM parameters to include the project's native library directory on the search path. You need to put the library in THAT directory. This wiki page explains what you need to do for NetBeans.
It looks like what you're really trying to do is load a native library. Try this:
public class MainClass {
static {
System.loadLibrary( "Your_native_lib_file_name" ); // Note: do not include the file extension!
}
}
You might also try -Djava.library.path=/exact/path/to/dir/
Answer copied from here:
Java can't seem to find my native libraries
This is a totally newbie question. I'm running Eclipse on Ubuntu. I created a test project that I want to compile to an executable (whataver the linux equivalent is of a Windows .exe file). Here's the contents of my program:
public class MyTest {
public static void main(String[] args) {
System.out.println("You passed in: " + args[0]);
}
}
I want to know how to compile it and then how to execute it from the command line.
Thanks!
You need to create an executable JAR file. Steps will follow shortly.
Right click on the project and select JAR file under Export.
Enter the path where you want it to be saved. The example here is of Windows. Change according to your platform.
Click Next.
Edit the Main Class field by browsing and selecting the location of your class that contains the main method.
Run it
To run the JAR file, open up a shell or command prompt and execute the following command:
java -jar path/to/test.jar
In Eclipse, choose file then export, and you will have to choose runnable jar. Also, you will be prompted to select the main class, MyTest in your case.
The Eclipes tutorials are very helpful, if you do the "create a Hello World application" tutorial it will walk you through the process of setup the project, building the app and running the jar file.
I want to know how to compile it ...
See other answers on how to get Eclipse to create a JAR file.
... and then how to execute it from the command line.
In the simple case, you execute it by running java -jar yourApp.jar <args>.
If your application depends on external libraries, then it is a bit more complicated ...
how come I would choose jar file over executable jar file?
Because a JAR file is portable, and a binary executable is not.
Because JIT compiled code runs faster than code that is compiled ahead of time.
Because the standard Java tool chain does not support creation of binary executables. Ditto for Eclipse, AFAIK.
Marked solution does not work.
Use "runnable" jar instead and then java -jar <jarfile>