exporting java jar help from Eclipse - java

so I have a project that only has one class Main:
public class Main{
public static void main(String[] args) {
System.out.println("asdf");
System.out.println(args[0]);
System.out.println(Integer.parseInt(args[1]));
}
}
so I tried to export this as a standalone jar...here's what I did
created a run configuration for the project...the run configuration runs properly from eclipse...it prints the arguments just fine...
right click on the project, selected export
choose the runnable jar file option
selected my previously created run configuration as the run configuration, chose export destination press finish, etc
but then when I run the program from the commandline:
Main.jar "testtest" 123
nothing came up.....it didn't print out the arguments....nor did it print out the "asdf" I specified in the main() function...
what did I do wrong?

Run java -jar Main.jar "testtest" 123 ?

I think you are getting wrong what exporting as a runnable jar means: This does not create an executable file. It just creates an archived file with all your compiled classes and some special meta information about which class to run when invoked with java -jar JARNAME.
Note that this also means all users of your program still need to have a java runtime installed.

Related

Java runs in eclipse and will compile, but wont execute on cmd, but still runs in eclipse. How can I get it to execute in cmd?

So I have a basic hello world set up in eclipse and I can compile it using cmd easily (I have set all the necessary paths), however when I then try to use the java command to execute the hello world, it always returns the same error:
Error: Could not find or load main class helloWorld
Caused by: java.lang.NoClassDefFoundError: net/codejava/helloWorld (wrong name: helloWorld)
This is the code used:
package net.codejava;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I am cd in the right directory (I think, I cd into the src directory and then into the package file stored in src) and am using Windows 10 with java 18.0.1 and JRE build 18.0.1+10-24
Any help would be greatly appreciated, as this is highly frustrating, when the code runs effortlessly on the eclipse console. Thanks.
Your file has a 'package' of net.codejava and a name of helloWorld, meaning, the full name of this class is net.codejava.helloWorld.
The java command, at least in the way you're using it, requires that you pass the full name, thus, you must run java net.codejava.helloWorld. Just java helloWorld simply isn't going to work.
But that's not all.
Java needs to then find the class file that contains the code for class net.codejava.helloWorld. It does this by first turning that full name into a path of sorts: net/codejava/helloWorld.class, and it will then scan each entry in the classpath for that. You can put directories and jar files on the classpath.
Thus, you have a directory on your system; let's call this directory X. X contains a directory named net, which contains a directory named codejava, which contains a file named helloWorld.class. If there is no such X (i.e. your class file is not in a dir named codejava for example), you're going to have to fix that by making these directories.
Then, X (and not the codejava dir!) needs to be on the classpath. Usually (it depends on how you configured things), 'the current dir' is by default on the classpath.
Given that your code is in, say, /home/PythonSux/workspace/learningjava/net/codejava/helloWorld.class, that means the dir that needs to be on the classpath is /home/PythonSux/workspace/learningjava. After all, if you, from there, look for net/codejava/helloWorld.class, you find the right file.
Therefore, either cd to that directory, or run java -cp /home/PythonSux/workspace/learningjava net.codejava.helloWorld
Note that this isn't usually how you actually run java apps. You either run them from your IDE, or you ask your build tool to run it, or you package your java app into a jar file and run that, etcetera.

How to create a executable jar file without any GUI component?

I have just created a program that uses file I/O and for interactive input and output it uses terminal window of IDE. I want to create a executable jar file of this program. Is it possible? If yes, then how?
I am using BlueJ as IDE.
The program have **no GUI component**
I have created a jar using BlueJ but it does not works.. When I double click on jar nothing happens...
You have to have an Main-Class entry in your Manifest. See documentation for details.
Given yours example
public class MainClass {
public static void main(String agrs[]) {
System.out.println("magic");
MainClass.interning_behaviour("12");
}
public static void interning_behaviour(String string_input) {
String string="12";
String string_5;
string_5=string_input.substring(0);
System.out.println(string==string_5);
}
}
1) You have to compile it: javac MainClass.java
2) You have to jar it jar cfe MainClass.jar MainClass MainClass.class
3) Now you can run it java -jar MainClass.jar
basically you should type:
jar cf jar-file input-file(s)
You can find usage details at link below:
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
It depends on the IDE you're using.
In eclipse for example you can do this by right-clicking the package in package explorer window and navigating Export ->Java -> JAR file.
Export as a runnable jar if you're in eclipse

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.

eclipse cannot find main method from given launch configuration

I went to eclipse>export>java>runnable jar file
I changed launch configuration to my project name; destination: desktop.
I tried all three library handling options:
extract required folders into generated JAR
Package required libraries into generated JAR
copy required files into a sub-folder next to the generated JAR.
I did not check anything to do with "ANT scripts".
When I pressed "finish" in the export window, eclipse gave me a error: "Could not find main method from given launch configuration". After pressing the "finish" button in the eclipse export window, the next button was grayed out. It created a JAR file at the destination. When I tried to open it Mac's Jar Launcher (default)(13.5.0), it gave me the message:
The Java JAR file "filename.jar" could not be launched.
Check the Console for possible error messages.
I googled this and some people said to check the manifest.mf file. I extracted it, then: "META-INF>manifest.mf". I opened manifest.mf with textedit.
Inside manifest.mf were the lines:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ acm.jar
Class-Path: .
I am running Mac OSx 10.6.8 snow leopard. I have the latest java available for snow leopard (I think my java is the one just BEFORE java 7).
Also, I'm trying to export a project with:
a .java file that contains all my code
acm.jar
java.policy.applet
Scores.txt
My .java file contains a public void run() {} and JFrames/JPanels. My .java file extends ConsoleProgram.
If there's no public static void main(String[] args) method, the virtual machine (Java) will not know where to start your program. It sounds like your run() method is what you want to start your program. If so, then add (to your main class (.java)) a method: public static void main(String[] args). Re-export your .jar, making sure the launch configuration is set to your Project name, and your main class.

Java Eclipse executable jar file

I am new to eclipse + Java. I am trying to create executable jar file with eclipse
export option. It works very well. But in my project, I have almost 10 packages (my own) and 4 main classes. I want to create a executable jar file that can execute any of main class from 4 main classes.
For example: Double click write class name and run that class
Dont use executable jar. Instead create a normal jar which will have compiled classes.
From command line, call whichever main class you want to call as a argument to the java jar command.
java -jar test.jar com.company.unit.MainClass1
java -jar test.jar com.company.unit.MainClass2
Executable JARs don't work that way. They write a manifest file in the JAR that declares where the main class is, and it runs that one. You would have to create 4 different JARs.
Alternatively, you can just create one main class that lets you type in which of the four you want, and then have it execute that one. Basically, you'd be mimicking the functionality that you are looking for on your own.
Just a quick example of how to deal with command line options to launch different things, I would have put it into a reply to #serplat's answer but then I can't format it.
public static void main(String[] args)
{
if(args.length == 0) {
// Do default here--no options specified
} else if(args.length > 2) {
// Complain that there are too many args, or implement multi-args
} else // known just one arg
if(args[1].equals("option1") {
// call the main of your first app
} else if(args[1].equals("option2") {
// start your second app
...
}
}
There are much better ways to handle command line stuff, but this is understandable and should do what you need. Later you might look into something more flexible.
I have recently made a tutorial that shows you how to create an executable jar file that will run with a double click in Windows, Mac OSX and Linux. For my project, I packaged a slick library game I had made. Hope it helps.
http://aramk.com/blog/2010/12/05/how-to-make-a-multi-platform-executable-java-jar-file/

Categories

Resources