IntelliJ build wrong JAR: Could not find or load main class - class

I have a simple example
public class FileSystemReadFile {
public static void main(String[] args) throws IOException {
System.out.println("Reading the file" + args[0]);
}
}
which is created in IntelliJ where I want to build JAR file; So what I did:
Added Artifact with dependencies (presumably I have some);
Ensure that MANIFEST.MF is located in src\main\resources\META-INF\ as it is already mentioned somewhere here on the site.
Run Artifact build which gave me JAR file in out folder and I run that jar file that said me "Could not find or load main class" java <name>.jar
You may see that main class is added into MANIFEST and location of manifest is also fine.
When I open that created JAR file, I see the same MANIFEST content, I see lots of dependency modules, but I don't see my class!
I suspect that is a cause. Any ideas?

If you include any signed JARs in your app and then use IntelliJ to build artifacts, it will extract the JARS and bundle them with your compiled output.
This then causes a JAVA security exception. I've seen this with Eclipse Paho and Bouncy Castle which are signed.
You can check if any of the library JARs you are using are signed using the jarsigner tool.
jarsigner -verify -verbose <path to library JAR>
Change your IntelliJ artifact setup so that these get bundled as libraries instead of being extracted. Extraction invalidates the certificate as you'd expect.
Try creating a dummy project with just Main. Add 1 library JAR (that you are trying to build with) at a time. Build an output JAR each time until Main breaks. That's how I found this.
IntelliJ should warn you.....

Not sure what was with IntellJ, but I rebuilded artifacts again and it was ok.
hadoop jar <Jar-name>
java -jar <Jar-name>
Everything is working fine.

Related

Build error deploying javafx application when added jna 4.1.0 library

I am trying to build my app, but I have the next error.
The jar libs\jna-4.1.0.jar has a main class com.sun.jna.Native that does not match the declared main com.bp.ocr.MainApp
I do not know how to solve it, I am looking for but I am not understand why happens.
Using base JDK at: C:\Program Files\Java\jdk1.8.0_31\jre
The jar libs\jna-4.1.0.jar has a main class com.sun.jna.Native that
does not match the declared main com.bp.ocr.MainApp
Bundler EXE Installer skipped because of a configuration problem:
Main application jar is missing.
Advice to fix: Make sure to use fx:jar task to create main application jar.
BUILD SUCCESSFUL

How to Correctly use com.mattbertolini.hermes.Hermes to manage Constants at Server side (GWT)?

Ok, I want to manage Constants at Server level, so com.mattbertolini.hermes.Hermes is a solution.
Please see all my steps:
1- I downloaded hermes-1.2.0.jar & import into correct Library of eclipse (no problem)
2- Create MyConstantsWithLookup.java at client package
import com.google.gwt.i18n.client.ConstantsWithLookup;
public interface MyConstantsWithLookup extends ConstantsWithLookup {
String myMsg();
}
3- create MyConstantsWithLookup.properties file in the same client package. The file has this line:
myMsg=Deleted
4- In ServerData.java in Server package
MyConstantsWithLookup my = Hermes.get(MyConstantsWithLookup.class, "");
String extra=my.myMsg();
When tested in eclipse it works fine but I got Warnign message in eclipse:
[WARN] Server class 'com.ibm.icu.util.ULocale' could not be found in the web app, but was found on the system classpath
[WARN] Adding classpath entry 'file:/C:/Users/eclipse-jee-juno-SR1-win32-x86_64/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.5.0.v201212122042-rel-r42/gwt-2.5.0/gwt-dev.jar' to the web app classpath for this session
For additional info see: file:/C:/Users/eclipse-jee-juno-SR1-win32-x86_64/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.5.0.v201212122042-rel-r42/gwt-2.5.0/doc/helpInfo/webAppClassPath.html
I am not sure what this warning's about? is it serious?
The code was running ok but why eclipse generated this warning?
This class (com.ibm.icu.util.ULocale) is a replacement for java.util.Locale, so it seems Hermes has a dependency on it.
Download the ICU4J jar from http://site.icu-project.org/download and add it to your project's build path.
Source: Matt Bertolini at https://github.com/mattbertolini/Hermes/issues/3
You need to add the ICU4j jar located at http://site.icu-project.org/
to your classpath. The reason you have the ICU classes inside the
gwt-dev.jar is because GWT includes them for its Java to JavaScript
compiler. This is regrettable since it often times falsely reports the
files on your classpath.

Classpath problems when running JUnit tests from Eclipse with Gradle build

I have a Gradle project that declares a test-only dependency on an XML data file, and then loads the file from the classpath. When I run the tests directly in Gradle from the command line, everything works fine, but when I run "gradlew eclipse", refresh the project in Eclipse, and then try running the test from Eclipse (Debug As -> JUnit Test), the test fails because it's unable to find the XML file and the classpath (as accessed from the Properties context menu item on the process in the Debug view) shows no indication of the XML file being included on the classpath.
The behavior I'm seeing has some commonality with http://gradle.1045684.n5.nabble.com/gradle-junit-tests-resources-and-classpath-td4418753.html#a4420758, but Sean's problem was the reverse: his tests ran properly under Ant (but he never mentioned trying to run directly from the Eclipse JUnit plugin), but not under Gradle.
Here's the relevant part of build.gradle:
dependencies {
testCompile group: 'com.mycompany', name: 'MyConfigFile', version: '0.0.0+dirty', ext: 'xml' }
Because the only resources that URLClasspathLoader can load directly from the file system are JARs, I'm using the following static method to search the classpath for files that match the filename I need to load:
public static String getFullPathForResourceDirectlyOnClasspath(String nameFragment) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
for (URL url: ((URLClassLoader)cl).getURLs()){
String fullPath = url.getFile();
if (fullPath.contains(nameFragment)) {
return fullPath;
}
}
return null;
}
I call that method as follows:
getFullPathForResourceDirectlyOnClasspath("/MyConfigFile-");
When I run that code from Gradle ("gradlew build"), it finds the file and my test succeeds. When I run it from Eclipse (Debug As -> JUnit Test), it fails to find that file on the classpath (because the Eclipse JUnit plugin doesn't put it there) and that call returns null.
I've tried changing the configuation from testCompile to compile to see if that made a difference, but it doesn't change anything (and perhaps tellingly, my .classpath doesn't have any entry for the XML file even when the compile configuration is selected).
Does anyone know of a way to make this work? Am I just missing something that should be obvious?
It seems that you are abusing the class path to pass a single argument (the absolute file path of the XML file) to a test. Instead, you should either put the XML file on the (test) class path in the correct way (it needs to go into a directory or Jar file that's listed on the class path) and load it correctly (e.g. with getClass().getClassLoader().getResource("some/resource.xml")), or pass the file path to the test as a system property. Naturally, the latter will be harder to make work for different environments (say Gradle build and IDEs).

"Package HttpClient does not exist" when compiling

I am attempting to compile EWSJavaAPI1.5 in Eclipse and in IntelliJ. I have had no luck. I keep getting a org.apache.commons.httpclient does not exist error. Its driving me nuts. I added the four required files:
commons-codec-1.4.jar
commons-httpcliient-3.1.jar
commons-logging-1.1.1.jar
jcifs-1.3.15.jar
The four jar files are being referenced, yet it does not find the httpclient when compiling. It may have to do with the class path but I am not sure if I am adding it to the class path correctly. What to do?
Check that the library classpath is set correctly and this library is added to the dependencies of your module. Another possible case could be corrupted jar file, try to download it again.

Hadoop throwing java.lang.VerifyError after exporting jar file

I'm altering a hadoop map - reduce job that currently compiles and runs fine without my changes.
As part of the job, I will now be connecting to S3 to deliver a file.
I developed a (very simple) s3Connector class, tested and ran it in eclipse,
then went to hook it into my reduce job. In order to run the job in hadoop, I have to export the project as a jar file, then call it from hadoop. The jar file seems to compile and export without problem from eclipse, but when I run it in hadoop, I get a java.lang.VerifyError exception.
java.lang.VerifyError: (class: com/extrabux/services/S3Connector, method:
connectToS3 signature: ()V) Incompatible argument to function
Several other posts mention that there may be jar version dependencies that are conflicting, but in my eclipse build path, I added all the latest jar files for the specified libs, and pushed them to the top of the build path order.
This is about as simple as I can isolate it down to:
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;
public class S3Connector {
protected RestS3Service s3Service;
protected AWSCredentials awsCredentials;
public S3Connector()
{
this.awsCredentials= new AWSCredentials("my secret 1", "my secret 2");
}
public void connectToS3() throws Exception
{
this.s3Service = new RestS3Service(this.awsCredentials);
}
}
Even that simple class will die.. Same message. As soon as I comment out the AWS credentials in the constructor and RestS3Service, the issue disappears. Basically, I think it's some kind of library export problem out of eclipse, but not sure how to find it.
Figured this out. There was an old version of the jets3t jar that was in the hadoop lib dir
the hadoop command line script loops over all jars in the lib dir and physically adds them to the classpath on the final exec'ed command line command that it builds. This command line classpath of the 0.6.0 jar was overriding the good 0.8.0 jar that I was exporting in my jar file. Since the 0.6.0 version did not have the specified constructor for RestS3Service , the java.lang.VerifyError was getting thrown. By removing the 0.6.0 lib from hadoop, all was well.