在Ubuntu上找不到Java应用程序主类 [英] Java Application main class not found on Ubuntu

查看:341
本文介绍了在Ubuntu上找不到Java应用程序主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gradle Application Plugin为Windows和Ubuntu / Linux / Mac等发布Java应用程序。

我在几周前在Linux上测试过它,它完美地工作。今天我的Linux主机没有在我的Linux机器上找到。我想不出我对Gradle部分所做的任何更改,除了从Gradle 2.0更新到2.12。我没有在Linux上测试每一项变化,因为我通常在Windows上工作。



Windows的批处理脚本完美无瑕,它不会弹出任何错误消息。在Linux上,我收到了经典的消息:

lockquote

无法找到或加载主类...


blockquote>

您知道任何可能导致Linux上出现此错误但不是Windows上出现此错误的情况吗?我认为Gradle应该构建完全相同的两个脚本,不需要设置任何特别的东西来使其在Linux上运行。如果我没有记错的话,我没有改变任何东西,因为更新Gradle,它在Linux上工作得很好。

  //我甚至将它改回旧的语法。 
//插件{
// id'java'//或'groovy'必须明确应用
// id'application'
//}

apply plugin:'java'
apply plugin:'application'

repositories {
mavenCentral()
}

mainClassName =controllers.Main

如果您需要更多信息,请不要犹豫,问我。



编辑



我更新到了IntelliJ 2016(来自最近几年的版本)。我不知道这是否重要,但我认为他们对Gradle做了一些改变。



编辑2



刚刚在OSX上进行了测试,功能完美无瑕。问题在于Ubuntu和Raspbian。

解决方案

花了几个小时尝试不同的事情后,我终于发现问题所在。

Ubuntu(和Raspbian)默认使用OpenJDK。我的应用程序的开发工作是在Oracle Java 8上完成的。



在Ubuntu上安装Oracle JDK后,它首先完成了工作。



我不知道这是否是预期的行为。我有两个问题:


  • 执行Java文件时,OpenJDK不应该有相同的参数吗?

  • 如果不是:Gradle是否应该支持OpenJDK?



解决方案完成后,我想发布一些信息,告诉您如何在Ubuntu(15.10)上轻松安装Oracle JDK 8: webupd8 存储库,它提供了一种自动化的方式在Ubuntu上安装Java 8(这也是Ubuntu / Canonical推荐的)。
$ b


我们的PPA支持Ubuntu 15.04,14.10,14.04和12.04以及Linux
Mint 17.1,17和13.使用以下命令添加PPA并安装Oracle Java 8(
包同时提供JDK8和JRE8):


blockquote>

  sudo add-apt-repository ppa:webupd8team / java 
sudo apt-get update sudo
apt-get安装oracle-java8-installer




提示:如果你在firewal l /路由器,用于阻止下载Oracle Java归档文件所需的
重定向,您可以手动下载JDK tar.gz归档文件并将其放在
/ var / cache / oracle- jdk8-installer - 然后,安装
oracle-java8-installer包将使用本地存档而不是
来尝试下载它。



安装完成后,在终端中运行java -version应该输出
,如下所示:



  andrei @ andrei-desktop:〜$ java -version 
java version1.8.0_31
Java™SE运行时环境(build 1.8.0_31-b13)
Java HotSpot™64位服务器虚拟机(构建25.31-b07,混合模式)


I am using the Gradle Application Plugin to distribute a Java Application for Windows and Ubuntu / Linux / Mac etc.

I tested this on Linux some weeks ago and it worked perfectly. Today my Main class is not found on my Linux machines. I can't think any changes I have made to the Gradle part, except for updating from Gradle 2.0 to 2.12. I didn't test every change on Linux because I usually work on Windows.

The batch script for Windows works flawless, it does not pop any error message. On Linux I get the classic message:

Could not find or load main class...

Are you aware of anything that could cause this error on Linux but not on Windows? I think Gradle should build both scripts exactly the same and I don't need to set up anything special to make it work on Linux. If I remember correctly I haven't changed anything since Updating Gradle, and it was working on Linux just fine.

// I even changed it back to the old syntax.
//plugins {
//    id 'java' // or 'groovy' Must be explicitly applied
//    id 'application'
//}

apply plugin: 'java'
apply plugin: 'application'

repositories {
    mavenCentral()
}

mainClassName = "controllers.Main"

If you need more infos dont hesitate to ask me.

EDIT

And I updated to IntelliJ 2016 (from the last years version). I don't know if this is important, but I think they did quite some changes with Gradle.

EDIT 2

Just tested this on OSX, and works flawless. The problem is just with Ubuntu and Raspbian.

解决方案

After spending hours trying different things, I finally found out what the problem is.

Ubuntu (and Raspbian) use OpenJDK per default. The development for my application was done on Oracle Java 8.

After installing the Oracle JDK on Ubuntu it worked on the first go.

I don't know if this is an expected behavior. I have two questions:

  • Shouldn't OpenJDK have the same parameters when executing a Java file?
  • If not: Shouldn't Gradle support OpenJDK?

To make my solution complete, I want to post some informations how you can install Oracle JDK 8 easily on Ubuntu (15.10):

I used the webupd8 repository which provides an automated way of installing Java 8 on Ubuntu (this is also recommended by Ubuntu/Canonical).

Our PPA supports Ubuntu 15.04, 14.10, 14.04 and 12.04 as well as Linux Mint 17.1, 17 and 13. Add the PPA and install Oracle Java 8 (the package provides both JDK8 and JRE8) using the following commands:

sudo add-apt-repository ppa:webupd8team/java 
sudo apt-get update sudo
apt-get install oracle-java8-installer

Tip: if you're behind a firewall / router that blocks some of the redirects required to download the Oracle Java archive, you can download the JDK tar.gz archive manually and place it under /var/cache/oracle-jdk8-installer - then, installing the "oracle-java8-installer" package will use the local archive instead of trying it to download it itself.

Once installed, running "java -version" in a terminal should output something like this:

andrei@andrei-desktop:~$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

这篇关于在Ubuntu上找不到Java应用程序主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆